Java NoClassDefFoundError occurring even when the class path is added - java

I'm trying to compile and run the PercolationVisualizer in https://github.com/samet/Coursera-Algorithms-I-Assignment-1. According to http://introcs.cs.princeton.edu/java/stdlib/, the required dependencies StdDraw.java and In.java should be present in the stdlib package.
After cloning the repository, I copied stdlib.jar to the src directory and then compiled PercolationVisualizer using the following command:
javac -cp .:stdlib.jar PercolationVisualizer.java
(I did the same for Percolation.java). However, when I try to run the program using java PercolationVisualizer, I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: In
at PercolationVisualizer.main(PercolationVisualizer.java:62)
Caused by: java.lang.ClassNotFoundException: In
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)
... 1 more
I don't understand the NoClassDefFoundError for In, since it should be included in stdlib. Can anyone explain this behavior?

If you are on Windows, use the semicolon as CLASSPATH separator ..
javac -cp ".;stdlib.jar" PercolationVisualizer.java Percolation.java WeightedQuickUnionUF.java PercolationStats.java
If you are on linux/*nix, use the colon as CLASSPATH separator ...
javac -cp ".:stdlib.jar" PercolationVisualizer.java Percolation.java WeightedQuickUnionUF.java PercolationStats.java
For more information look here
If you want to debug the process of compilation, use the "-verbose" flag. It will display you CLASSPATH, the "lodaing jar_file ..." statements. If it has not recognized your jar file, it will now show the "loading" statement.

I finally unjar'd stdlib.jar into the working directory, after which the compilation worked. (This is indeed the first method to use stdlib described in http://introcs.cs.princeton.edu/java/stdlib/).

Related

Question about java.lang.NoClassDefFoundError: quickfix/MessageStoreFactory

I have a problem when I run my Java application for a FIX application. I have added all the QuickFIX/J jars to build the FIX application. But when I run my jar I always get this error:
Application: TestMarketdataRequest
Exception in thread "main" java.lang.NoClassDefFoundError: quickfix/MessageStoreFactory
at com.dxtr.fastmatch.FastmatchMDRequest.<init>(FastmatchMDRequest.java:14)
at com.dxtr.fastmatch.FastmatchMDRequest.main(FastmatchMDRequest.java:19)
Caused by: java.lang.ClassNotFoundException: quickfix.MessageStoreFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 2 more
My question: How do I fix this and why do I get this error when I did add QuickFIX/J in my depedencies?
Include the dependency jar in your classpath when you run the main class using the -cp option:
java -cp quickfixj.jar MainClass
-classpath classpath
-cp classpath
Specifies a list of directories, JAR files, and ZIP archives to search for class files. Separate class path entries with semicolons (;). Specifying -classpath or -cp overrides any setting of the CLASSPATH environment variable.
More on the docs.
this is closed. i have add executable jar in my pom. so it is closed right now thanks all

JNI error has occurred when running java with OpenCV from terminal

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

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

Jar file with Jaudiotagger, NoClassDefFoundError

I made a java class using the library Jaudiotagger, and it runs without problems. I then made a jar out of it and I got NoClassDefFoundError. Here's how it went:
I put the main class id3tag.java and the library jaudiotagger-2.0.3.jar in a folder and compiled using the command line. The program ran smoothly without problems.
javac -cp .;jaudiotagger-2.0.3.jar id3tag.java
java -cp .;jaudiotagger-2.0.3.jar id3tag
I then created the manifest and the jar file.
echo Main-Class: id3tag >manifest.txt
jar cvfm id3tag.jar manifest.txt id3tag.class jaudiotagger-2.0.3.jar
I got the following output:
added manifest
adding: id3tag.class(in = 5952) (out= 2997)(deflated 49%)
adding: jaudiotagger-2.0.3.jar(in = 811441) (out= 740599)(deflated 8%)
I then ran the jar file, and got "A Java Exception has occurred.". I also tried:
java -jar id3tag.jar
And I got the output:
Exception in thread "main" java.lang.NoClassDefFoundError: org/jaudiotagger/tag/
FieldDataInvalidException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2451)
at java.lang.Class.getMethod0(Class.java:2694)
at java.lang.Class.getMethod(Class.java:1622)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: org.jaudiotagger.tag.FieldDataInval
idException
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 6 more
I then replaced all (both) error classes used from Jaudiotagger with Exception and recreated the jar. Now the same thing happens as for seemingly all other jar files when I run them (with Java(TM) Platform SE binary, or through writing id3tag.jar in the command prompt) - nothing. java -jar, however, works and gave me a runtime error.
Exception in thread "main" java.lang.NoClassDefFoundError: org/jaudiotagger/audi
o/AudioFileIO
at id3tag.tagSong(id3tag.java:112)
at id3tag.tagAlbum(id3tag.java:82)
at id3tag.tagArtist(id3tag.java:40)
at id3tag.main(id3tag.java:170)
Caused by: java.lang.ClassNotFoundException: org.jaudiotagger.audio.AudioFileIO
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 4 more
It seems like the entire library just doesn't do shit for me. How do I fix this, and how do I get jar files to run without java -jar? (Also, how do I get the full pile'o'errors in case someone needs it, rather than just having it say "x more"?)
I'm not very savvy with this kinda shit so the more specific the answer, the better. Thanks.
I'm on windows 8 and latest java (1.7.0_17).
this is informing you that you have no main class, as you might or might not already know all programs in almost all non web based programming languages must have a main class to start from. In java it looks like this
public static int main(String[] args){
}
The message is informing you that this class is no longer there, this could be a compiler error or something entirely different.
please try downloading the library again, and downloading eclipse ide for java ee developers.
this ide has a built in compiler that will work better than a command line sometimes.
i hope this helped.
I worked around it by including the org folder from jaudiotagger in my .jar instead of the actual .jar file, and I then used a .bat file to run it instead of running the .jar directly. Feel free to add your own answer if you find a better solution, and I'll check back.
I extracted jaudiotagger-2.0.3.jar with winrar so that the org folder was in the same folder as my main class. Then I could compile and run the main class simply by:
javac id3tag.java
java id3tag
I then created the manifest file the same way as before, and created the jar:
jar cvfm id3tag.jar manifest.txt id3tag.class org
The jar file worked with java -jar id3tag.jar, but simply writing id3tag.jar still did nothing. Turns out it's because jar files are by default run by the javaw.exe file, so you have to right click the jar file -> open with -> choose default program and navigate to the java.exe file in your java folder (just search the folder). Running the .jar then gets you "Error: Could not find or load main class path\id3tag.jar". I worked around this by using a .bat file. I entered
java -jar path\id3tag.jar
into Notepad and saved as whatevername.bat (save as type: all files).
I created a new question for getting the .jar file to work properly. See .jar error - could not find or load main class.

Works in sbt but not raw Java: after using `javac` to compile a .class file, why can't `java` find it?

I used ObjectWeb's ASMifier to get a 'HelloDump.java', and added classloader and a main method to load and run a spoofed "HelloWorld".
If I run 'HelloDump.java' in the build tool sbt, everything works fine and outputs "HelloWorld!".
But if I use raw Java, it breaks. 'HelloDump.java' seems to compile OK, but the resulting 'HelloDump.class' is clearly present, yet doesn't seem to be recognized (check the sequence below):
$ julianpeeters#julianpeeters-virtual-machine ~/asm-example $ javac -cp lib/asm-all-4.1.jar HelloDump.java
$ julianpeeters#julianpeeters-virtual-machine ~/asm-example $ ls
DumpLoader.java.bak HelloDump.class.bak Hello.java.bak
Hello.class.bak HelloDump$DynamicClassLoader.class lib
HelloDump.class HelloDump.java README.md
$ julianpeeters#julianpeeters-virtual-machine ~/asm-example $ java -cp lib/asm/all/4.1.jar HelloDump
Exception in thread "main" java.lang.NoClassDefFoundError: HelloDump
Caused by: java.lang.ClassNotFoundException: HelloDump
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: HelloDump. Program will exit.
Explicitly adding . to the classpath doesn't help either.
So why does this work in sbt, but not in raw Java, and how can I fix it?
Thanks, any advice is appreciated,
-Julian
Add . to your CLASSPATH, so your command becomes:
java -cp lib/asm-all-4.1.jar:. HelloDump
The reason the JVM cannot find the class is that it looks only on the classpath and nowhere else.

Categories