JNI error has occurred when running java with OpenCV from terminal - java

I'm trying to compile a java-program written with OpenCV 3.2.0 from the terminal. It was originally written in Eclipse with OpenCV as a user library.
I compile it with this command:
javac -cp /opencv-3.2.0-java/build/bin/ *.java
It works with no error, the -cp command shows the path to the OpenCV jar. I then try to run everything with this command:
java -Djava.library.path="/opencv-3.2.0-java/build/lib" -cp jar/opencv-320.jar -classpath . Main
It gives me this error:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/opencv/core/Mat
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.opencv.core.Mat
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
I searched for solutions to this problem and came to the conclusion that it had something to do with native libraries. I use the -Djava parameter to link to the path of the library files from OpenCV, but it still doesn't work somehow. How do I fix this?
EDIT:
All the .java files are on a folder on the desktop. This is were the Main file is included. For example:
home/pi/Desktop/MultiSensor_v2.0_Java/Main.java
The path to the OpenCV jar:
/home/pi/Desktop/MultiSensor_v2.0_Java/opencv-3.2.0-java/build/bin/opencv-320.jar
The path to OpenCV native libraries:
/home/pi/Desktop/MultiSensor_v2.0_Java/opencv-3.2.0-java/build/lib

-cp is short for -classpath. So I suspect that when you use both, the second one overwrites the first.
You can have multiple entries on the class path separated by : as stated in the documentation: Setting the class path. (actually, it looks like that is a typo in the doc, and it should be ; as a separator. You could try both)
If you execute your command from the directory home/pi/Desktop/MultiSensor_v2.0_Java/ that would make:
java -Djava.library.path="opencv-3.2.0-java/build/lib" -cp .:opencv-3.2.0-java/build/bin/opencv-320.jarr Main

Related

Java cannot find javax/vecmath/Vector3d.class, even with jar in classpath

My platform has ubuntu 18.04.3 and openjdk version "1.8.0_242".
I'm running someone's executable jar file which references javax.vecmath.Vector3d, but the the openjdk vm generates NoClassDefFoundError for that class, as displayed below. The javax class is installed on my system in /usr/share/java/vecmath.jar, I've verified that it contains javax.vecmath.Vector3d, so I provide that jar in the '-cp' option:
% java -cp /usr/share/java/vecmath.jar -jar cameraCalc.jar
But I get this error:
java -cp /usr/share/java/vecmath-1.5.2.jar -jar cameraCalc.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: [Ljavax/vecmath/Vector3d;
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: javax.vecmath.Vector3d
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:419)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:352)
... 7 more
Why can't the jvm find that class even though it's in the vecmath.jar provided as a '-cp' option?
Thanks
Tom
My problem was due to running an executable jar file that does not contain all needed classes. From the documentation:
When you use this option, the JAR file is the source of all user
classes, and other user class path settings are ignored.
So "-cp" options will be ignored when running an executable jar file.

How to use Jlex and Cup tools

i'm trying to learn something about JFlex and Cup tools on Ubuntu, because i have to use them for a school project. So i downloaded and installed JFlex and Cup. There are some examples in "jflex-1.6.1/examples", so i tried to run one of them. In particularly i tried to run the one in "jflex-1.6.1/examples/cup", following the instructions in the README file:
To compile:
jflex lcalc.flex
java java_cup.Main < ycalc.cup
javac Main.java
To run:
java Main test.txt
Files:
"Main.java" demo of a main program
"Makefile " makefile to compile and test the example
"README"
"lcalc.flex" the lexer spec
"output.good " how the output should look like for the test
"ycalc.cup" the parser spec
"test.txt " sample input for testing
With many difficulties, i have been finally able to compile, then to complete successfully the first three instructions. Now i have errors when i try to run: java Main test.txt. In particular i get this error:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: java_cup/runtime/Scanner
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: java_cup.runtime.Scanner
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
I don't have any idea on how to solve this. I tried also with other examples but the results were the same.
I hope that you can help me. Thank you!
I also got this error, and it's because I forgot to include java-cup.jar and java-cup-runtime.jar in my class path.
Try compiling like this:
javac -cp path/to/java-cup.jar Main.java
And then run your program like this:
java -cp path/to/java-cup-runtime.jar Main

Compiled JAVA project, can't make it work

got a problem, can't manage it to work for me right.
Completed my java project and i want it to compile and run for a test. I do compile it as "Jar file" (not runnable jar file). I compile it through Eclipse IDE.
I do not setup any more references in compiling settings, making everything stock. Once i compile it as "Server.jar" in my desktop i'm trying to run through console, because it's more like console program. I'm using macOS.
Using command : java -jar Server.jar
Error:
Ervinass-MacBook-Air:Desktop Ervinas$ java -jar Server.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/parser/ParseException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.json.simple.parser.ParseException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
What should i do?
It shouws class is not found if i'm right?
This : org/json/simple/parser/ParseException is not recognised.
What i have tried : java -classpath ~/Documents/Java/Server/src/server/json-simple.jar -jar Server.jar
As all of you see, i'm using json-simple.jar library, moreover i do use mysql library, so i guess later it will be the same problem with mysql library.
Any solution? Tired of getting it working now..

Import package from subfolder: Exception in thread "main" java.lang.NoClassDefFoundError

I have this directory structure:
src
MyPackageFile1.java
MyPackageFile2.java
vendor
something.jar
Example.java
Note that all files under src have a package com.example; declaration, while Example.java doesn't belong to that package: it is an example of usage of that package (so it has import xyz.pushpad.*;).
I successfully compile with:
javac -classpath vendor/something.jar src/*.java Example.java
However when I run:
java Example
I get:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: com/example/DeliveryException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: com.example.MyPackageFile1
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
If MyPackageFile1.java and MyPackageFile1.java are declared in package com.example, then there are three problems:
Your source directory structure is wrong. It should be this:
src
com
example
MyPackageFile1.java
MyPackageFile2.java
You are using the wrong classpath separator. Since you are using Linux (based on the pathname syntax), the classpath separator is : not ;.
Classpath you are using to run the code is wrong. The java command line should be something like this:
java -cp vendor/something.jar:src:. Example
However, this is all very hacky.
Unless your codebase needs to be modularized as multiple code trees, then you should put your Example source code into the src directory; e.g.
src
Example.java
com
example
MyPackageFile1.java
MyPackageFile2.java
It is a bad idea to put your source code and compiled code into the same tree. A better idea is to use the -d option; e.g.
mkdir bin
javac -cp bin:vendor/something.jar -d bin
src/Example.java
src/com/example/MyPackageFile1.java
src/com/example/MyPackageFile12java
java -cp bin:vendor/something.jar Example
You should be using a build tool of some kind ... if you want your builds to be consistent and reproducible.
Similar to the compile command the run command also needs the jar file on the classpath
java -cp vendor/something.jar;src com.example.Example
where Example.java is moved to new directory src/com/something

JAVA: After adding all dependent jars to -cp, still getting errors

I'm working on a project(on Linux) with Lucene, and it works just fine in Eclipse after adding all external jars to build path. However, when I tried to compile them manually I got this error:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/lucene/analysis/standard/StandardAnalyzer
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
Here are the external jars I'm using in Eclipse:
I already tried these:
(java -cp "jar1:jar2..." class-built-in-eclipse)
java -cp "../../lucene-4.0.0/core/lucene-core-4.0.0.jar:../../lucene-4.0.0/queryparser/lucene-queryparser-4.0.0.jar:../../lucene-4.0.0/queries/lucene-queries-4.0.0.jar:../../lucene-4.0.0/analysis/common:../../IK Analyzer 2012FF_hf1/IKAnalyzer2012FF_u1.jar:." MyIndex
(java -cp jar1:jar2...:"the one that has whitespace in its path" class-built-in-eclipse)
java -cp ../../lucene-4.0.0/core/lucene-core-4.0.0.jar:../../lucene-4.0.0/queryparser/lucene-queryparser-4.0.0.jar:../../lucene-4.0.0/queries/lucene-queries-4.0.0.jar:../../lucene-4.0.0/analysis/common:"../../IK Analyzer 2012FF_hf1/IKAnalyzer2012FF_u1.jar":. MyIndex
and (moving the dependent jars to the same folder as the class file)
java -cp . MyIndex
Move the dependencies (your jar files) into a single folder (say lib), then you can use something like
java -cp "lib/*":. MyIndex

Categories