title: Why does an gnu.io.PortInUseException pop up intermittently, but never on the first run of my application?
body: <p>I'm using a bunch of custom made hardware (RFID readers) and talking to them using NRJavaSerial 3.11.0. My target OS is El Capitan and the target machines are either mac minis or imacs running El Capitan 10.11.1 through 10.11.4, which are meant to run unattended. Here's the code I use to connect to the device(s).</p>
  
  <pre><code>public RFIDReader(String portName,int baudRate, int resetTimerInMillis) throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException, IOException
  {
      portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
      if ( portIdentifier.isCurrentlyOwned() )
      {
          System.out.println("Error: Port is currently in use");
      }
      else
      {
          commPort = portIdentifier.open(this.getClass().getName(),2000);
  
          if ( commPort instanceof SerialPort )
          {
              serialPort = (SerialPort) commPort;
              serialPort.setSerialPortParams(baudRate,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
              serialPort.setOutputBufferSize(50);
              serialPort.setInputBufferSize(25);
              if(resetTimerInMillis!=0)
              {
                  serialPort.enableReceiveTimeout(resetTimerInMillis);
              }
  
              in = serialPort.getInputStream();
              out = serialPort.getOutputStream();
  
  
          }
      }
      System.out.println("Connection complete: " + portName);
  
  }
  </code></pre>
  
  <p>On exit, this code is run</p>
  
  <pre><code>public void close() throws IOException
  {
      //this.stopReadingUltraLight();
  
      in.close();
      System.out.println("Inputstream closed.");
      out.close();
      System.out.println("Outputstream closed.");
      serialPort.removeEventListener();
      System.out.println("Event listeners removed.");
      serialPort.close();
      System.out.println("serial Port closed.");
      commPort.close();
      System.out.println("Comm port closed.");
  }
  </code></pre>
  
  <p>The first run of the application works just fine, as intended. Usage of the serial device is through the in and out stream being used in many loops. However, occasionally, when I exit the application, it seems to break the port. Subsequent starts of the application show this exception:</p>
  
  <pre><code>gnu.io.PortInUseException: Unknown Application
      at gnu.io.CommPortIdentifier.open(CommPortIdentifier.java:475)
      at main.java.RFIDReader.&lt;init&gt;(RFIDReader.java:48)
      at main.java.RFIDBridge.&lt;init&gt;(RFIDBridge.java:151)
      at main.java.WebHandler.lambda$0(WebHandler.java:379)
      at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:182)
  ...
  </code></pre>
  
  <p>Once this error turns up, no application (coolterm etc.) can use the device until it's physically disconnected and reconnected. Coolterm throws an "error 83". <code>lsof | grep usbmodem</code> shows no process currently owning the device. I've also made the <code>var/lock</code> directory and run <code>chmod 777</code> on it. I'm kinda at the end of my rope here and can't figure out where the ownership of the device is locked up. Any ideas?</p>
  
  <p>RFIDReader.java:48 refers to the line <code>commPort = portIdentifier.open(this.getClass().getName(),2000);</code> in my device connecting section.</p>