No dhtreader in java.library.path - java

I try to get this maven projekt to run on the Raspberry Pi 3 but the DHT11 sensor gives me the following error:
sudo java -classpath '.:classes:*:classes:/opt/pi4j/lib/*' com.epam.llpd.greenhouse.web.ServerStarter
java.lang.UnsatisfiedLinkError: no dhtreader in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1864)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at com.epam.llpd.greenhouse.sensor.Sensor.<clinit>(Sensor.java:16)
at com.epam.llpd.greenhouse.PiHardwareController$1.run(PiHardwareController.java:132)
In the folder \src\main\java\com\epam\llpd\greenhouse\sensor there is the dhtreader.c file.
I did compile a binary objekt with the following command:
gcc -o dhtreader.so -lc -shared -I/usr/lib/jvm/jdk-7-oracle-arm-vfp-hflt/include -I/usr/lib/jvm/jdk-7-oracle-arm-vfp-hflt/include/linux dhtreader.c -l bcm2835
The dhtreader.so is placed in the same folder as the .jar.
The dhtreader.c file is a common C class specific for the Raspberry Pi to use the DHT11 and DHT22 sensor and it is used in many Raspberry Pi projects and has alot of sources like this one.
This C class (dhtreader.c) is used via. JNI/C but it just doesn't work because of the error above.
I allready tried to set the java.library.path with the following method in the java main:
System.setProperty("java.library.path", "./");
That didn't help. Instead i added -Djava.library.path=./ to the java command:
sudo java -classpath '.:classes:*:classes:/opt/pi4j/lib/*' com.epam.llpd.greenhouse.web.ServerStarter -Djava.library.path=./
But that didn't help neither.
Now i'm stuck and don't know how to add the dhtreader.so to the java.library.path.
How do i add the dhtreader.so to the java.library.path?
I asked in the Raspberry Pi StackExchange first but they thought that it's off-topic.

Try to simply rename dhtreader.so to libdhtreader.so.
Indeed when we call System.loadLibrary("foo") in java the expected name of the library is:
Windows: foo.dll
Unix/Linux: libfoo.so
Mac: libfoo.jnilib
Try to launch it as next:
sudo java -Djava.library.path=./lib -classpath '.:classes:*:classes:/opt/pi4j/lib/*' com.epam.llpd.greenhouse.web.ServerStarter
NB: The command above assumes that your lib is located in the lib directory that is itself located in the directory where the command has been launched.

Related

How do I use a library on a exported runnable jar?

I've got a java script that uses OpenCV 4.6.0 to modify some pics. I use Eclipse to edit the java project. I downloaded the OpenCV library (a opencv_460.jar file) which is used inside Eclipse. When I run the code, it works just fine. So I export the java project as a runnable jar.
The thing is that I want to use the jar on a different machine (ubuntu 18.04). When I run java -jar TheExportedJar.jar, I get the following error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java460 in java.library.path: /usr/java/packages/lib:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib
I installed OpenCV 4.6.0 on the machine using CMake
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.6.0.zip
unzip opencv.zip
mkdir opencv-4.6.0/build
cd opencv-4.6.0/build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PKGCONFIG=ON ..
make -j$(nproc)
sudo make install
The version is ok, everything looks fine, except it doesn't actually get installed. Things that should exist, like alternative path /usr/local/lib/pkgconfig/opencv4.pc, don't actually exist
How does java work? Why can Eclipse use a jar as a library, but I can't once the project is packed? Is there a way to skip the installation process (as every type of install I've tried was just a waste of time) and import a jar from a specified path as a library inside the java code?
What other solutions could there be?

weka, command line, setting path, java

I'm new to Weka. I downloaded it and can use the GUI interface, but I cannot figure out why I cannot run it from the command line (on a Mac OS X).
I'm in bash. When I do echo $PATH, I can see the path to weka.jar, but when I run
java -cp weka.jar weka.classifiers.functions.MultilayerPerceptron
Error: Could not find or load main class weka.classifiers.functions.MultilayerPerceptron
Caused by: java.lang.ClassNotFoundException: weka.classifiers.functions.MultilayerPerceptron
Now, I go to the weka folders and I can see that I have classifiers/functions/ there, but the files inside are only htmls.
I also tried
java weka.core.WekaPackageManager
Error: Could not find or load main class weka.core.WekaPackageManager
Caused by: java.lang.ClassNotFoundException: weka.core.WekaPackageManager
You need to set the classpath
export CLASSPATH=/home/weka/weka.jar:$CLASSPATH
I use this bash script to start wekafrom the command line:
#!/bin/bash
WEKA_HOME=/opt/smallapps/weka-3-9-1
export WEKA_JAR=$WEKA_HOME/weka.jar
export PATH=$WEKA_HOME:$PATH
CP="/usr/share/java/:/opt/dbvis9/jdbc/jtds/jtds.jar:/opt/database/sqlite/sqlite-
jdbc.jar:$WEKA_JAR"
echo "used CLASSPATH: $CP"
DIR=$WEKA_HOME
echo "changing to '$DIR'"
cd "$DIR"
export WEKA_HOME
# start small GUI Chooser, with 8GB of max memory
java -cp $CP -Xmx8092m weka.gui.GUIChooser &
#java -cp $CP -Xmx500m weka.gui.GUIChooser &
This also includes jarfiles with database drivers to the classpath
I think not all of these variables WEKA_HOME, WEKA_JAR, DIR etc are necessary, it's just what I'm using at the moment.

File missing from java.library.path

I am building an app to get NMEA GPS data, with serial port communication via RXTX.
On my Mac it runs great. When I try to run it on my Rasp Pi, it throws the error:
no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver
On the Mac, I see that if I move /Library/Java/Extensions/librxtxSerial.jnilib it too throws the same error.
My question is: On my RasbPi, do I need to add this rxtxSerial file somewhere to a folder (perhaps within the JVM?), or should it be packaged up with the runnable jar when I export. Is my problem on my Mac, or on my RasbPi?
Thanks
You can install this library on the Raspberry Pi via the normal package installation method.
Just type:
sudo apt-get install librxtx-java
Now you can start the jar like this:
java -Djava.library.path=/usr/lib/jni -cp /usr/share/java/RXTXcomm.jar -jar your.jar
The error means that a native library named rxtxSerial cannot be found. You need to have a native library for the Raspberry Pi with that name, and set the system property java.library.path so that it points to the directory that contains the native library. That means you have to start your Java program with the -D option, for example:
java -Djava.library.path=/some/dir com.mypackage.MyProgram
where /some/dir is the directory that contains the native library.
You'll have to find out if there is a native library that is suitable for the Raspberry Pi for the RXTX library that you are trying to use.

Java Path zeromq setting

I am using MAC OSX
I already follow the instruction on 0mq official site to install
when i compile it , i dont get any error.
But when i run the following command
java -classpath /Users/john/jzmq/ -cp $(lein classpath) storm.starter.WordCountTopology
I got the following error.
java.lang.UnsatisfiedLinkError: no jzmq in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1878)
at java.lang.Runtime.loadLibrary0(Runtime.java:849)
at java.lang.System.loadLibrary(System.java:1087)
at org.zeromq.ZMQ.<clinit>(ZMQ.java:34)
at storm.starter.spout.RandomSentenceSpout.nextTuple(RandomSentenceSpout.java:39)
at backtype.storm.daemon.executor$fn__3985$fn__3997$fn__4026.invoke(executor.clj:502)
at backtype.storm.util$async_loop$fn__465.invoke(util.clj:377)
at clojure.lang.AFn.run(AFn.java:24)
at java.lang.Thread.run(Thread.java:724)
My setting
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home
I also have zmq.jar under
/usr/local/lib and
/usr/local/share/java
I read through all the google search I can found. Still no crew why my one is not working
In addition to setting the JAR classpath, you need to specify where the jzmq .so library files reside. Did you build zmq, libzmq, and jzmq exactly as described in the directions? If so, you should see the .so library files in /usr/local/lib and zmq.jar in /usr/local/share/java, confirm that, then logout/login based on this, then try this:
java -Djava.library.path=/usr/local/lib -cp "/usr/local/share/java/zmq.jar:/Users/john/jzmq/<your jar here>" storm.starteer.WordCountTopology
Hope it helps

UnsatisfiedLinkError when using JNI?

I want to call a C program from Java program using JNI in linux ubuntu.
I am new to this and I have tried the sample program given in http://www.ibm.com/developerworks/java/tutorials/j-jni/section2.html
. I have already created the .java, .h , .c and .so files. But when i tried to run the program I am getting the following error.
Exception in thread "main" java.lang.UnsatisfiedLinkError: no Sample1 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1738)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1028)
at Sample1.main(Sample1.java:13)
This exception is indicating that the .so is not available to the JVM.
Adding the directory where the .so exists to the LD_LIBRARY_PATH will resolve this. If the .so depends on other .so libraries the directories where these .so exist will also need added to LD_LIBRARY_PATH.
I've just tried to get the same sample to work on my CentOS and got the same error as you. As already answered, JVM failed to find the so file needed. I succeeded to get it to work by following the steps below using gcc:
$ javac Sample1.java
$ javah Sample1
$ # Include paths must also be specified using -I option in the following gcc command line!
$ gcc -shared -I...snip... Sample1.c -o libSample1.so
$ # Library path for libSample1.so must also be specified!
$ java -Djava.library.path=...path/to/libSample1.so... Sample1
If you omit the "lib" prefix of the shared library, JVM fails to find it for some reason.
I don't know why. I am not familiar with the naming convention of shared libraries in Linux.
I hope this post could help.

Categories

Resources