问题
Basically i wanted to write a simple communication tool for my Arduino using the RXTX Library for java and failed horribly when it came to loading the dynamic library.
My system specs:
OS: OS X Yosemite 10.10.3
Java: SDK 1.8.0_45
RXTX: 2.1-7r2 - modified version for intel mac running 64 bit java, which can be found here.
IDE: NetBeans 8
I checked, that these files work by following the install instructions, which is simply copying these two files into the /Library/Java/Extensions directory.
Now I wanted to remove them and load the library from my application. I did the following:
- Add the RXTXcomm library to the project in netbeans.
- Includ the container of the native library in the "java.library.path" property using the following piece of code
System.setProperty("java.library.path",
location.getPath() + File.pathSeparator + System.getProperty("java.library.path"));
- And load the library using
System.loadLibrary("rxtxSerial")
When I compiled the code and tried to run it, it gave me an
java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path
Now I'm wondering what i have done wrong (maybe some great misunderstanding?)
Any help would be appreciated!
回答1:
This an explanations to your problem and a suggestion to a solution.
RXTX has two major problems in my opinion:
- Depending on your IDE, you need to place the Mac:
RXTXcomm.jar
andlibrxtxSerial.jnilib
PC:RXTXcomm.jar
,rxtxSerial.dll
on the root of the project in your IDE or Java code, it varies from IDE to IDE. The documentation here does not cover how to do it in different IDE like NetBeans, IntelliJ even thus I got it to work on both Eclipse and IntelliJ, but not NetBeans yet. I still have other issues. - Depending on your OS, even if you get this package up and run, in Windows 8.1 as example, it has problem closing the port. And the only solution is to restart you IDE/console and reconnect.
Any way I suggest you going over to a more problem less package called JSSC
Here is a simple reading data from serial port using JSSC:
public class Main {
public static void main(String[] args) {
SerialPort serialPort = new SerialPort("COM1");
try {
serialPort.openPort();//Open serial port
serialPort.setParams(9600, 8, 1, 0);//Set params.
byte[] buffer = serialPort.readBytes(10);//Read 10 bytes from serial port
serialPort.closePort();//Close serial port
}
catch (SerialPortException ex) {
System.out.println(ex);
}
}
}
- See all examples of JSSC codes
- The package can be downloaded here.
- JSSC home page
Note: This is an open answer, if any one of you have experience regarding this please contribute by editing the answer. I have seen people asking question and having almost same problem with RXTX I general.
回答2:
I am having the same issue, but believe that I know the cause.
I previously had RXTX functioning on OSX (Maverick) but found that I required functionality only available within the Java 8 JDK. I installed this over the Java 6 JDK that was on the machine and once completed was unable to run any code utilizing the RXTX library.
I believe the issue is that JDK releases after 6 do not include a 32 bit environment which is what RXTX requires.
If you are using a newer JDK then I would recommend installing the Java 6 JDK and seeing if this resolves your issue. When previously running my compiled programs I would need to include "-d32" to specify to utilize the 32 bit environment. ie: "java -d32 test"
回答3:
In addition to my recent reply this one.
This is the code to delete the mentioned cache:
try {
Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
fieldSysPath.setAccessible(true);
fieldSysPath.set(null, null);
} catch (NoSuchFieldException |
SecurityException |
IllegalArgumentException |
IllegalAccessException ex) {
ex.printStackTrace(System.err);
}
So the issue was not the rxtx library but rather my own failure to read the java doc. Because I'm sure this is mentioned somewhere right at the beginning.
来源:https://stackoverflow.com/questions/31215821/java-rxtx-library-doesnt-load-native-library