Export a runnable JAR and include .so native library - java

I'm writing a java application (on Linux OS) which accesses the webcam and display a video on a JPanel. I'm using openCV libraries in order to obtain it (also because I would like to add some image processing code).
I followed this guide to compile the openCV library for Java
and the only way I could make it work was by creating an user library linked to the folder where I have the corresponding jar file.
(I tried to directly import the jar file but I have some runtime exception, so I gave up).
This is my situation.
Now I have a working application (if I execute it from within Eclipse), and I want to export it as a single jar (or something executable outside Eclipse). I tried to achieve this by selecting these options.
The problem is that now I want test if the jar works by executing the following command from terminal (where Untitled.jar is the name of the exported code):
java -jar Untitled.jar
but I receive this error (I bolded the main problem):
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498) at
org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.UnsatisfiedLinkError: no opencv_java331 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
myRefreshTest.Main.main(Main.java:8)
opencv_java331 is the name of the .so library generated after building openCV for Java (opencv_java331.so)
I tried several way to export my program, also by selecting the 3rd option in the Eclipse export popup ("Copy required libraries into sub-folder next to the generated JAR) and then executing
java -cp Untitled_lib/opencv-331.jar -jar Untitled.jar
but terminal returns this error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no
opencv_java331 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
myRefreshTest.Main.main(Main.java:8)
So, the problem is clear, it can't find the openCV .so library.
I tried to use also option -Djava.library.path but it is not working (or at least I'm not using it properly)
I'm not a Java expert and I cannot understand how to fix this problem. Does anyone know how to fix this issue?

Related

opencv native library cannot be found

I am using an exmplae from here to apply face recognition using opencv. I have extracted opencv at C:\. However, after running the provided example cod in Java, I get the following exception:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java341 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1865)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1119)
at detectingfaceinanimage.DetectingFaceInAnImage.main(DetectingFaceInAnImage.java:23)
I used System.out.println(System.getProperty("java.library.path")); to locate path for the java library but I have received numerous paths. Can you let me know how to fix thsi issue? (For Windows platform)
To solve this issue, needed to copy the .dll file from C:\opencv\build\java\x64 (as I am running a 64 bit operating system) into C:\Program Files\Java\jdk1.8.0_31\bin.
You should do these things:
Add the OpenCV Jar file in your project classpath (I think you
already did it)
Locate the OpenCV DLL file location where you extracted it, maybe it
looks like this:
C:\opencv\build\x64\vc15\bin
Then add this path in your system environment variable
In the end, restart your IDE (not your computer), and yes it is that!
Good Luck!!

Including libraries generating jar and executing with java -jar on Intellij

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.

Compiling and running in java. Giving me errors

compiled my project in netbeans IDE and when I run the jar nothing happens just gives me a error occured while trying to run it. I think I found the problem just not sure how to fix it. Help please!
This is what happens when I check the jar in CMD
C:\Users\Mac\Desktop>java -jar dist\pong.jar
Exception in thread "main" java.lang.UnsatisfiedLinkError: no lwjgl in java.libr
ary.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at org.lwjgl.Sys$1.run(Sys.java:73)
at java.security.AccessController.doPrivileged(Native Method)
at org.lwjgl.Sys.doLoadLibrary(Sys.java:66)
at org.lwjgl.Sys.loadLibrary(Sys.java:95)
at org.lwjgl.Sys.<clinit>(Sys.java:112)
at org.lwjgl.opengl.Display.<clinit>(Display.java:135)
at mw.Main.initDisplay(Main.java:36)
at mw.Main.main(Main.java:25)
C:\Users\Mac\Desktop>
When I check the manifest file it's pretty much empty:
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build
when I check the jar in CMD C:\Users\Mac\Desktop>java -jar dist\pong.jar
You need to use the -cp switch while running the jar using the java command.
Documentation
You can easily google to find more examples on how does -cp works.
You need to create a library and add a reference to it in your project
See Setting Up LWJGL with Netbeans
At a guess I'd say you are missing step 5 from Setting Up a Project to Use LWJGL in NetBeans
Finally, you need to tell NetBeans where the Native Libraries (i.e, DLL, JNILIB, DYLIB, SO files) are for your system so that the natives are linked when running.
Select the Run category and then type the following into the VM Options, replacing what is in bold to suit your system:
-Djava.library.path=<lwjgl-X.X path>/native/<linux|macosx|solaris|windows>
Update: Didn't realise you were doing it from the command prompt. You say you are running
java -jar dist\pong.jar
You need to add the -D option so something like this:
java -Djava.library.path=<lwjgl-X.X path>/native/windows -jar dist\pong.jar

Exception in thread "main" java.lang.UnsatisfiedLinkError: no jpcap in java.library.path

i am using netbeans ide and trying to capture packets using jpcap library. my program used to run correctly before bu suddenly this error
Exception in thread "main" java.lang.UnsatisfiedLinkError: no jpcap in java.library.path
is appearing. i have tried ds on my other computer too but in vain ..
i have added jpcap library and before installing i cleared older version completely. have done everythng available .i am seriously in trouble
please help!
Add this to the beginning of your program:
System.out.println(System.getProperty("java.library.path"));
You should get a list of directories. Your jpcap's DLL (or so on UNIX) must be in one of these directories in order for your program to work. If it is not there, then either:
Add the directory containing that DLL/so to your system's PATH (preferred).
Add the DLL/so into one of the directories printed.

Exception in thread "main" java.lang.UnsatisfiedLinkError: no Terminal in java.library.path

I was trying to run the CHARVA Tutorial thru Eclipse after setting up the JAR,
but I receive
Exception in thread "main" java.lang.UnsatisfiedLinkError: no Terminal in java.library.path
in the console. Any clue on how to fix this up?
As Dave said, Charva requires a native library called Terminal to run. As far as I remember, it is included in the binary distribution for Charva.
Eclipse-specific instructions: In your project properties, select Java Build Path, then Libraries. Locate the jar and set the Native library location to the path where the Terminal library is.

Categories

Resources