RXTX works with drivers that depend on the OS it's running. These drivers are provided with RXTX library and they are:
librxtxSerial.so
andlibrxtxParallel.so
for Linux.librxtxSerial.dll
andlibrxtxParallel.dll
for Windows.librxtxSerial.jnilib
for Mac
This link has the instructions to properly install these drivers and this question: java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path has several workarounds to solve this kind of exception.
However all of them assume that you have access to %JAVA_HOME% path or you know where the drivers are located which is fine if you're working on your computer but it's not ok if you need to distribute your app. If this is the case, just copy the driver files in the same folder where your jar
is placed. You have to see something like this to know it's working properly:
Stable Library ========================================= Native lib Version = RXTX-2.1-7 Java lib Version = RXTX-2.1-7
Update
Recently I've noticed that Linux doesn't include the current working directory in java.library.path
property. Consequently if you simply add librxtxSerial.so
into app folder you still get UnsatisfiedLinkError
. Once again, you can set this property when you run the application through command line like this:
java -jar -Djava.library.path=/path/to/lib
Or you can use a hacky approach explained here: Setting "java.library.path" programmatically. This approach is unsafe and I don't recommend it if you have access to implement another workaround, but I've tried this and it works:
String javaLibraryPath = System.getProperty("java.library.path");
if(!javaLibraryPath.contains(File.pathSeparator + "." + File.pathSeparator)){
StringBuilder sb = new StringBuilder(javaLibraryPath).append(File.pathSeparator).append(".");
// The line above appends "." (current directory) to the library path
// Of course 'rxtxSerial.so' must be placed in the current directory
System.setProperty("java.library.path", sb.toString());
try {
Field sysPathsField = ClassLoader.class.getDeclaredField("sys_paths");
sysPathsField.setAccessible(true);
sysPathsField.set(null, null);// Setting this field to null will force a reevaluation of
// the library path as soon as loadLibrary() is called
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex) {
ex.printStackTrace();
}
}