3

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!

redxef
  • 492
  • 4
  • 12
  • You should have following 3 files on your project root RXTXcomm.jar, rxtxParallel.dll, rxtxSerial.dll it is different from IDE to IDE where to place these files. Let me know if that helps. So I can make a answer with guideline to it. – Maytham Fahmi Jul 04 '15 at 01:09
  • Well, I'm on a mac, thus i only have 2 files: RXTXcomm.jar and librxtxSerial.jnilib. (There is no parallel native lib for mac when downloading the precompiled files) – redxef Jul 04 '15 at 01:15
  • No problem, the concept is the same, I have had the same problem with windows, the issue is, the when compiling from your IDE, it need to know the location of the files, I think if you find out to place them in the correct root folder location of your project it will solve the problem. Otherwise I would suggest you go over to JSSC package it is better https://code.google.com/p/java-simple-serial-connector/ – Maytham Fahmi Jul 04 '15 at 01:18
  • I double and triple checked the path, it's the correct one, that's what I've got after loading the directory: file:/Users/redxef/NetBeansProjects/arduino_io/dist/lib:/Users/redxef/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:. and in this first directory are both files – redxef Jul 04 '15 at 01:20
  • OK, try jssc, because my experience with RXTX is not very good, have had a lot of issues. Maybe JSSC will solve it for you. – Maytham Fahmi Jul 04 '15 at 01:22
  • Thanks for pointing me to that one, going to have a look on it. – redxef Jul 04 '15 at 01:25
  • np, examples https://code.google.com/p/java-simple-serial-connector/wiki/jSSC_examples – Maytham Fahmi Jul 04 '15 at 01:29
  • any progress with the new package? – Maytham Fahmi Jul 04 '15 at 18:47
  • I tried to use a library and succeeded, it was as simple as adding the library to the project (really nice!). But it seems, that jssc is buffering the output and I haven't found out how to flush it. – redxef Jul 04 '15 at 21:49
  • Is that the case if you did use the Event mask & SerialPortEventListener interface? – Maytham Fahmi Jul 05 '15 at 00:02
  • It seems I was too stupid and didn't give my Arduino enough time to respond, thats why I didn't get anything back and assumed, that the output was buffered. But thanks for your help! – redxef Jul 05 '15 at 11:38
  • ofc, do so, but it might be that I can only check it after 10 days or something. – redxef Jul 10 '15 at 14:24

3 Answers3

1

This an explanations to your problem and a suggestion to a solution.

RXTX has two major problems in my opinion:

  1. Depending on your IDE, you need to place the Mac: RXTXcomm.jar and librxtxSerial.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.
  2. 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);
        }
    }
}

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.

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
1

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"

  • I just recently found the issue: Since Java only adds Libraries on Launch it doesn't change anything if you change them when the virtual machine is already up and running. A workaround is to delete the cache or to make the executable relaunch itself with the correct arguments. – redxef Nov 11 '15 at 22:33
0

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.

Community
  • 1
  • 1
redxef
  • 492
  • 4
  • 12