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.
Related
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?
So I built a vision library on windows, and I've ran it on Windows and it ran okay. I used the command:
java -jar LiftTracker.jar
I transferred the .jar file I built on windows over to a Raspberry Pi, and did a make install to install the opencv libraries. Once I did that, I tried to do the same command as above and came up with the error:
java.lang.UnsatisfiedLinkError: no opencv_java310 in java.library.path.
I did some research and found that I could run this command along side the -jar command
java -Djava.library.path=/path/to/dir
That still did not work. Is it the way that I am importing the system library? The way I'm importing it in the code is by:
static{
System.loadLibrary("opencv_java310");
}
I think the main reason that it's not working is because of the way I installed opencv. Any ideas?
Thanks!
You need to add "libopencv_java320.so" to your java project libs. It is around 1mb additional library.
You can generate this .so file from sources as per documentation: https://opencv-java-tutorials.readthedocs.io/en/latest/01-installing-opencv-for-java.html#install-opencv-3-x-under-linux
another way is to build sources manually using terminal cmake (it will download around 4gb of opencv sources), should be easy: download the source from opencv: http://opencv.org/releases.html Unzip it and inside unpacked directory create a /build directory like this ../opencv-3.2.0/build/. Make sure you have cmake installed (Debian/Ubuntu apt get install cmake). Open terminal in previously created /build folder and type: cmake -DBUILD_SHARED_LIBS=OFF .. after operation finishes type make -j8 and after that "libopencv_java_320" should be generated for 3.2.0 version - copy this .so into your java project. Last type make install from the same build directory to install 3.2.0 libs on the system (you might want to previously remove older version if necessary). More info here: https://elbauldelprogramador.com/en/compile-opencv-3.2-with-java-intellij-idea/
same as above approach but automated will be by using this script: https://github.com/milq/milq/blob/master/scripts/bash/install-opencv.sh Script does also install opencv on the linux system. Took it from this source: http://milq.github.io/install-opencv-ubuntu-debian/ It does much more then 2nd approach, should be easiest to make.
After installing opencv libs in system and copying libopencv_java320.so into your java project you can remove sources (it is almost 4gb after all).
Then you can use below code in your main method to load windows .dll (if you previously added it too) and linux .so:
String libName = "";
if (SystemUtils.IS_OS_WINDOWS) {
libName = "opencv_java320.dll";
} else if (SystemUtils.IS_OS_LINUX) {
libName = "libopencv_java320.so";
}
System.load(new File("./libs/".concat(libName)).getAbsolutePath());
if you builded OpenCV on OS;
1) set opencv and java variable
JAVA_HOME = the directory containing your JDK
ANT_HOME = the directory in which Apache Ant is installed
OPENCV_HOME = the directory in which all of OpenCV is installed
OPENCV_LIB = the directory containing all native JNI libraries
OPENCV_JAR = the path to the JAR file containing the java interface
to OpenCV (typically named something like "opencv-320.jar" )
OPENCV_HOME will be at /home/opencv-3.2.0
OPENCV_JAR will be at ${OPENCV_HOME}/build/bin/opencv-320.jar
OPENCV_LIB will be at ${OPENCV_HOME}/build/lib
2) Load Native Library
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
3) Run your application
java -Djava.library.path=${OPENCV_LIB} -jar myapp.jar
https://github.com/WPIRoboticsProjects/GRIP-code-generation/tree/master/java
I've been trying to execute a jar file through command line but I'm still getting an exception that I'm missing a library even though it was properly added as it's shown here. The library that I'm trying to run my code with is the opencv's lib. I'm able to run my app from intellij and generate a jar artifact that I can see the jar grows in size when I add the opencv lib but when I try to run the app from the command line with "java -jar Test.jar" I get the message that I'm missing the opencv library.
rinaldi#rinaldi-work:~/Projects/Test/out/artifacts/Test_jar$ java -jar Test.jar Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java310 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at HelloWorld.<clinit>(HelloWorld.java:14)
at Main.main(Main.java:4)
I've also tried to run the app adding the path to the opencv's library:
:~/Projects/Test/out/production/Test$ java -cp .:~/Sources/opencv-master/build/bin/opencv-310.jar Main
And another attempt because opencv needs not only it's jar but also the /opencv-master/build/lib/ directory:
:~/Projects/Test/src$ java -cp :/home/rinaldi/Sources/opencv-master/build/bin/opencv-310.jar:/home/rinaldi/Sources/opencv-master/build/lib/ Main
Below are the images from intellij's configuration.
I had to add the library this way:
java -jar -Djava.library.path=/home/rinaldi/Sources/opencv-master/build/lib test.jar
Hope this helps someone.
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
I am trying to build and run an example jni program. The program is just a sample helloworld program. I did not write it but I assume that it works. I am running this on Linux. There are four files.
HelloNative.c
HelloNative.h
HelloNative.java
HelloNativeTest.java
To build the files, I did
gcc -I/myDir/jdk/include -I/myDir/jdk/include/linux -fPIC -c HelloNative.c
gcc -shared -o HelloNative.so HelloNative.o
java *java
Here is the result of the build
HelloNative.c
HelloNative.h
HelloNative.o
HelloNativeTest.class
HelloNative.class
HelloNative.java
HelloNative.so
HelloNativeTest.java
Then I did
setenv LD_LIBRARY_PATH /myDir/myExample:${LD_LIBRARY_PATH}
java HelloNativeTest
I got the following error
Exception in thread "main" java.lang.UnsatisfiedLinkError: no HelloNative in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1734)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1028)
at HelloNative.<clinit>(HelloNative.java:9)
at HelloNativeTest.main(HelloNativeTest.java:8)
I checked the LD_LIBRARY_PATH and the HelloClassTest and HelloNative.so, they were all there. I tried to specify the -CLASSPATH also, but that did not seem to matter.
Does anyone have any ideas ?
Do the following, where X="HelloNative".
Give the library a filename following a system-dependent standard. On Linux, name your library libX.so.
Set the java.library.path System property to the directory containing your library.
Call System.loadLibrary("X") where "X" is the cross-platform part of the library name above.
You named your library HelloNative.so; change it to libHelloNative.so.
From http://download.oracle.com/javase/6/docs/technotes/guides/jni/spec/design.html#wp679:
The argument to System.loadLibrary is
a library name chosen arbitrarily by
the programmer. The system follows a
standard, but platform-specific,
approach to convert the library name
to a native library name. For example,
a Solaris system converts the name
pkg_Cls to libpkg_Cls.so, while a
Win32 system converts the same pkg_Cls
name to pkg_Cls.dll.
If you use OSGi in the future, there's an alternative to setting java.library.path.
You can also try by setting the java.library.path:
java -Djava.library.path=/myDir/myExample HelloNativeTest
Did you do a System.loadLibrary() from Java?