如何在 Ubuntu 20.04 上使用 libRXTX 修复 java.lang.ClassNotFoundException:gnu.io.CommPortIdentifier?

如何在 Ubuntu 20.04 上使用 libRXTX 修复 java.lang.ClassNotFoundException:gnu.io.CommPortIdentifier?

我有以下错误:

$ sudo java -jar /home/fadedbee/tc65sh-1.4.0/bin/tc65sh.jar -Djava.library.path=/usr/lib/jni/ -cp /usr/share/java/RXTXcomm.jar -p /dev/ttyUSB0 -b 115200 -f push.txt
tc65sh v1.4.0
processing command file push.txt
connecting /dev/ttyUSB0 with 115200 baud, flowControl r
Exception in thread "main" java.lang.NoClassDefFoundError: gnu/io/CommPortIdentifier
    at org.tc65sh.device.Device.connect(Device.java:56)
    at org.tc65sh.Main.exec(Main.java:102)
    at org.tc65sh.Main.main(Main.java:59)
Caused by: java.lang.ClassNotFoundException: gnu.io.CommPortIdentifier
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 3 more

我已经安装了 libRXTX,并且所有文件都出现在正确的位置:

$ ls -lA /usr/lib/jni/
total 148
-rw-r--r-- 1 root root 22696 May  9  2018 librxtxI2C-2.2pre1.so
lrwxrwxrwx 1 root root    21 May  9  2018 librxtxI2C.so -> librxtxI2C-2.2pre1.so
-rw-r--r-- 1 root root 14424 May  9  2018 librxtxParallel-2.2pre1.so
lrwxrwxrwx 1 root root    26 May  9  2018 librxtxParallel.so -> librxtxParallel-2.2pre1.so
-rw-r--r-- 1 root root 22688 May  9  2018 librxtxRaw-2.2pre1.so
lrwxrwxrwx 1 root root    21 May  9  2018 librxtxRaw.so -> librxtxRaw-2.2pre1.so
-rw-r--r-- 1 root root 22704 May  9  2018 librxtxRS485-2.2pre1.so
lrwxrwxrwx 1 root root    23 May  9  2018 librxtxRS485.so -> librxtxRS485-2.2pre1.so
-rw-r--r-- 1 root root 59824 May  9  2018 librxtxSerial-2.2pre1.so
lrwxrwxrwx 1 root root    24 May  9  2018 librxtxSerial.so -> librxtxSerial-2.2pre1.so

$ ls -lA /usr/share/java/ | grep RXTX
-rw-r--r-- 1 root root   63027 May  9  2018 RXTXcomm-2.2pre2.jar
lrwxrwxrwx 1 root root      20 May  9  2018 RXTXcomm.jar -> RXTXcomm-2.2pre2.jar

我可以在 jar 文件中看到正确的类:

$ strings /usr/share/java/RXTXcomm.jar | grep CommPortIdentifier
gnu/io/CommPortIdentifier.class
gnu/io/CommPortIdentifier.classPK

我怎样才能帮助 Java 找到该类?

(我已经尝试将选项顺序更改为,sudo java -Djava.library.path=/usr/lib/jni/ -cp /usr/share/java/RXTXcomm.jar -jar /home/fadedbee/tc65sh-1.4.0/bin/tc65sh.jar -p /dev/ttyUSB0 -b 115200 -f push.txt但错误仍然存​​在。)

答案1

解决方案是使用:

$ sudo java -Djava.library.path=/usr/lib/jni/ -cp /usr/share/java/RXTXcomm.jar:/home/fadedbee/tc65sh-1.4.0/bin/tc65sh.jar org.tc65sh.Main -p /dev/ttyUSB0 -b 115200 -f push.txt

它解决了第一个问题,但现在我只剩下:

$ sudo java -Djava.library.path=/usr/lib/jni/ -cp /usr/share/java/RXTXcomm.jar:/home/fadedbee/tc65sh-1.4.0/bin/tc65sh.jar org.tc65sh.Main -p /dev/ttyUSB0 -b 115200 -f push.txt
tc65sh v1.4.0
processing command file push.txt
connecting /dev/ttyUSB0 with 115200 baud, flowControl r
RXTX Warning:  Removing stale lock file. /var/lock/LCK..ttyUSB0
initializing device
forcing obex mode
java.io.IOException: obex response timeout after 5001 ms and 33 bytes
    at org.tc65sh.device.Device.receiveObexResponse(Device.java:543)
    at org.tc65sh.device.Device.openObexMode(Device.java:326)
    at org.tc65sh.device.Device.obexOpenObexMode(Device.java:127)
    at org.tc65sh.Main.exec(Main.java:110)
    at org.tc65sh.Main.main(Main.java:59)

相关内容