public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("/dev/cu.usbserial-FTCC026H")) {
System.out.println("Connecting.....");
// if (portId.getName().equals("/dev/term/a")) {
ReadReadings reader = new ReadReadings();
}
}
}
}
public ReadReadings() {
try {
serialPort = (SerialPort) portId.open("ReadReadings", 1000);
} catch (PortInUseException e) {System.out.println(e);}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {System.out.println(e);}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {System.out.println(e);}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();serialPort.close();
}
br = new BufferedReader(new InputStreamReader(inputStream));
} catch (UnsupportedCommOperationException e) {System.out.println(e);}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
Thread.sleep(10000);
if(inputStream != null)
{
serialPort.close();
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
serialPort.close();
ConnectToHardware.frame.dispose();
frame = new DisplayReadings();
//frame.pack();
frame.setVisible(true);
}
} catch (InterruptedException e) {System.out.println(e);serialPort.close();}
}
public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
// byte[] readBuffer = new byte[1024];
// String temp = "";
try {
String inputLine=br.readLine();
series.put(num, inputLine);
num++;
System.out.println(inputLine);
}
catch (IOException e) {System.out.println(e);}
break;
}
}
}
我需要创建我的项目的runnable jar文件,其中包括3个类,外部rxtx库和project中的librxtxSerial.jnilib。但是在创建runnable jar文件时,此librxtxSerial.jnilib文件未包含在jar文件中。我认为这是运行jar文件时无法获得输出的原因。
答案 0 :(得分:0)
JNI库由操作系统加载。因此,它们不能直接从jar文件加载。它们是从文件系统加载的(有关详细信息,请参阅java.load.path。)
您可以尝试在java代码中集成安装程序,该代码在加载本机代码之前将jar文件中的lib复制到文件系统中。