2

I put rxtxcomm.jar into jre/lib/ext folder, but I still get NoClassDefFoundError Isn't this folder automatically taken into the global classpath?

Thanks

Jagger
  • 10,350
  • 9
  • 51
  • 93
Tobia
  • 9,165
  • 28
  • 114
  • 219
  • The only reasonable answer would be that the `JRE` you are running your program with is not the same `JRE` you put the `jar` file in. – Jagger Oct 09 '12 at 21:14
  • could you post **exception stacktrace**. it is possible that you have put RXTXcomm.jar in the right place, but you hadn't provided the path to the external libraries – linski Oct 09 '12 at 21:32
  • pls see update, you might go better with jssc, depending on you needs – linski Oct 10 '12 at 08:34
  • I have the same problem -> http://stackoverflow.com/questions/36762416/build-issues-rxtx-library-on-wildfly-10-0 .If you resolved yours, any help would be appreciated. – Mxsky Apr 21 '16 at 11:51
  • I refactored my project with JSSC and I solved all problems. – Tobia Apr 21 '16 at 11:52

1 Answers1

2

yes it is taken automatically to classpath, but RXTXcomm uses JNI /native external libraries (.so and .dll files), you must provide the path to them when running your program in command line:

java -jar yourprogram.jar -Djava.library.path="PATH_TO_EXTERNAL_LIBRARIES"

for linux:

suppose you unpacked the rxtx.zip to /home/user/

if you have 32bit x86 platofrm:

PATH_TO_EXTERNAL_LIBRARIES = /home/user/Linux/i686-unknown-linux-gnu/

if you have 64bit x86 platform the it would be:

PATH_TO_EXTERNAL_LIBRARIES = /home/user/Linux/x86_64-unknown-linux-gnu/

for windows:

suppose you downloaded and unpacked it to C:\rxtxt

PATH_TO_EXTERNAL_LIBRARIES = C:\rxtxt\Windows\i368-mingw32\

If you find it cumbersome to do it from command line you can do it from yout code (before opening port via RXTXcomm):

System.setProperty("java.library.path","PATH_TO_EXTERNAL_LIBRARIES");

EDIT:

of course, you must put RXTXcomm.jar in your classpath in addition to all of the above. If running from command line as jar packaged program - yourprogram.jar - inside the jar you must have a META-INF folder that contains MANIFEST.MF with the following entries:

Class-Path: lib/RXTXcomm.jar
Main-Class: pkg.Main

and yourprogram.jar must be in folder which has folder lib in which is RXTXcomm.jar, also the class with

public static void main(String[] args)

method must be called Main and reside in package named pkg (just replace pkg.Main with whatever you have).

Then you can run your program succesfully and open a serial port if you have one. This approach also eliminates the need to copy anything in jre/lib/ext folder

EDIT^2:

or, if you don't want to pack your program in jar, position yourself in folder which contains the folder pkg and write:

java -cp PATH_TO_RXTX/RXTXcomm.jar -Djava.library.path="PATH_TO_EXTERNAL_LIBRARIES" pkg.Main

(all paths can be relative or absolute)

EDIT^3:

but I would recommend Java Simple Serial Connector instead of RXTXcomm:

  • it handles heavy load from multiple threads as opposed to RXTXcomm (tested in production)
  • external libraries are packed in jar so there is no need for setting java.library.path
linski
  • 5,046
  • 3
  • 22
  • 35