12

Thanks in advance..

I have a project that uses opencv-300.jar as external library. I have tried this in eclipse and in natBeans. In both it is working successfully when I am running my project from IDE itself. I want my project to export it as a runnable (or executable) jar. I placed my opencv_java300.dll file in source folder with main java file and given its name in

 System.loadLibrary("opencv_java300");

I placed opencv-300.jar in external jar libraries and all other files which are needed in Main program. it is working successfully when running from IDE but when I am creating executable jar, it shows an error

   Exception in thread "main" java.lang.UnsatisfiedLinkError: no     
   opencv_java300 in
   java.library.path
   at java.lang.ClassLoader.loadLibrary(Unknown Source)
   at java.lang.Runtime.loadLibrary0(Unknown Source)
   at java.lang.System.loadLibrary(Unknown Source)
   at CropFaceImage.main(CropFaceImage.java:27)

Please tell me Is there any way to give java.library.path in program itself. My project is working sucsessfully even when I have removed path for opencv_java300.dll file in external library.

Anuj
  • 241
  • 1
  • 2
  • 6

5 Answers5

12

I tried to pass the command which contains path for opencv but I found no other way. Somehow i tried something which created my jar and it is properly running. I copied the opencv_java300.dll file and put it in the directory which is next to the my jar file and did same for all supporting files. I used following code to do so.

String opencvpath = System.getProperty("user.dir") + "\\files\\";
String libPath = System.getProperty("java.library.path");
System.load(opencvpath + Core.NATIVE_LIBRARY_NAME + ".dll");
Anuj
  • 241
  • 1
  • 2
  • 6
  • 1
    This was not working correctly for me. I used load() rather than loadLibrary() and prepended the Core.NATIVE_LIBRARY_NAME by the path of the DLL file. Finally, the .dll extension is added at the end to form the absolute path of the DLL file. The line I used is "System.load("D:\\OpenCV\\opencv-3.0.0\\opencv\\build\\java\\x64\\"+Core.NATIVE_LIBRARY_NAME+".dll");" Remember to use x64 or x86 depending on your system. I did not copied the DLL file. – Ahmed Gad Jun 08 '19 at 18:39
5

You can use a command line argument as below and call your class which has the main

java -Djava.library.path="Folder which contains your dll" ....
Kalyan Chavali
  • 1,330
  • 8
  • 24
  • 1
    Does this work when creating jar file command prompt...Please tell how can i create jar file including external library ( such as other jar file..opencv-300.jar) with this command. – Anuj Jul 08 '15 at 08:07
  • 1
    This works when executing your class from the prepared jar file. Whenever you are invoking the class in the jar file, you would need to pass above argument. – Kalyan Chavali Jul 08 '15 at 08:08
  • 1
    I want to create a runnable jar file which does not take any argument ..Is there any way to give this command in program itself or when creating jar file from command prompt. – Anuj Jul 08 '15 at 08:12
  • 1
    I am not sure if it is possible . You can however, write a bat file which inturn calls the runnable jar with the above argument . You can use the bat file instead of the runnable jar. – Kalyan Chavali Jul 08 '15 at 08:22
  • It is showing an error could not find or load main class CrooppingTool2.jar – Anuj Jul 09 '15 at 10:55
  • What are you trying to invoke and can you provide the class name and package of your main calls ? – Kalyan Chavali Jul 09 '15 at 11:25
  • I run this command java -Djava.library.path="D:\myFolder\" myProject.jar. it shows an error could not find or load main class. But shows no opencv_java300 in java.library.path when run jar from command prompt in usual way. java -jar myProject.jar – Anuj Jul 09 '15 at 12:51
1

I had the same problem, solved it by switching the JRE System library. It seems like the problem occurs only if using jre1.8.0_65. Everything worked well by me with jre1.8.0_25, jre1.8.0.45 and jre1.8.0.66

nik_kobe
  • 35
  • 1
  • 7
0

I solved my problem when I've configured native library in eclipse. You need choose a library reference your OS platform.

Look at here: adding openCV to java buildpath in eclipse.

Community
  • 1
  • 1
0

I was able to fix the error by removing my System.loadLibrary("opencv_java300"); From the code and adding the jar file to the classpath in my build.xml:

<jar destfile="program.jar" basedir="build/classes">
    <manifest>
        <attribute name="Main-Class" value="com.src.program"/>
        <attribute name="Class-path" value="opencv-300.jar"/>
    </manifest>
</jar>
AldaronLau
  • 1,044
  • 11
  • 15