简体   繁体   中英

OpenCV java project runs in Eclipse but not in Command line

I downloaded OpenCV for Java and added the jar and libraries to the project in Eclipse and it works fine.

But when I try to run the .class file where my main(String args[]) is, it throws

> java MainActivity 1 5 8
Exception in thread "main" java.lang.NoClassDefFoundError: org/opencv/core/Core
    at HoleFiller.<init>(HoleFiller.java:29)
    at MainActivity._fixHole(MainActivity.java:100)
    at MainActivity._getImageNames(MainActivity.java:92)
    at MainActivity._authArgTypes(MainActivity.java:66)
    at MainActivity.main(MainActivity.java:17)
Caused by: java.lang.ClassNotFoundException: org.opencv.core.Core
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 5 more

It crashes on this line

System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

Is there any way to include the .jar file in the project so it will run from the command line?

Thanks in advance

When you run within Eclipse, then the IDE will automatically handle the dependencies, but when you run from the commadnd line, then you need you to handle it your self.

Follow these commands to do so:

  1. When compiling:

javac -cp path/to/opencv.jar YourProgram.java

  1. When running:

java -cp path/to/opencv.jar:. -Djava.library.path=path/to/opencv.dll YourProgram

And you can add the .dll path to the system path, then it will automatically get access to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM