Getting NoClassDefFoundError for an included jar - java

I'm writing a java app and running it on a linux ec2 server.
the program was running fluently yesterday, and after I changed and organazied some files and packages I'm getting this problem when I execute the program:
I'm running the programm with this command:
java -cp . main.Server
And get the following error:
Exception in thread "Thread-0" java.lang.NoClassDefFoundError: org/json/JSONObject
at main.Group.<init>(Group.java:28)
at main.Server.run(Server.java:23)
Caused by: java.lang.ClassNotFoundException: org.json.JSONObject
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:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 2 more
I was running the programm as this before the changes:
export CLASSPATH=$CLASSPATH:/home/ec2-user/java-json.jar
In the bin folder, command:
java Server
And the programm was running.
Since I changed the package from default name to main, I need to run it with the command java -cp . main.Server, but I get the error written above.
I also tried modifing the .bash_profile and add this:
CLASSPATH=$CLASSPATH:/home/ec2-user/java-json.jar
export CLASSPATH
But it didn't help.
I have no errors in my code and the jar is not broken since it worked before. What should I do to fix this problem?

Specify the jar file on the classpath like this:
java -cp .:/home/ec2-user/java-json.jar main.Server
If your classpath is indeed correctly set in your .bash_profile, you could also do:
java -cp .:$CLASSPATH main.Server

Related

Runtime error loading Interface definition in Java using Apache HTTP library

I am attempting to make a simple wrapper around an API connection in Java. I am using the org.apache.http library to do this.
I have downloaded the following jar files:
org.apache.http httpclient saved as ./org.apache.httpclient.jar
org.apache.http httpcore saved as ./org.apache.httpcore.jar
I can then successfully compile the file with:
$ javac -cp "./org.apache.httpclient.jar:./org.apache.httpcore.jar" Api.java
However when running it with $ java Api I receive:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/HttpEntity
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2625)
at java.lang.Class.getMethod0(Class.java:2866)
at java.lang.Class.getMethod(Class.java:1676)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: org.apache.http.HttpEntity
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:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 6 more
[It turned out the issue was with how code was being run after compilation; the code has been removed to avoid distracting from the actual problem and solution]
I can't see why the system has a problem finding the HttpEntity interface when it finds other imports successfully, and can compile. If I remove any of the import lines compilation fails with unknown symbol errors.
-cp should contain the directory where you classes are, for example:
java -cp "./org.apache.httpclient.jar:./org.apache.httpcore.jar:." Api
Assuming that your Api.class is in the current directory. (replace : by ; as classpath separator if your are on a windows system)
Have you tried running the java with '-classpath'. While compiling you are giving the classpath, so you need to add the classpath option for running the class file as well.
More details can be found at below link:
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html
Also you can print which libs are in classpath using below command.
java -cp $(for i in lib/*.jar ; do echo -n $i: ; done). com.test.Class

ClassNotFoundException when running hadoop jar

I'm attempting to run a MapReduce job from a jar file and keep getting a ClassNotFoundException error. I'm running Hadoop 1.2.1 on a Centos 6 virtual machine.
First I compiled the file exercise.java (and class) into a jar file exercise.jar using the following shell script compile.sh :
#!/bin/bash
javac -classpath /pathto/hadoop-common-1.2.1.jar:\
/pathto/hadoop-core-1.2.1.jar /pathto/exercise.java
jar cvf exercise.jar /pathto/*.class
This runs fine and the jar completes successfully. I then attempt to run the actual MapReduce job using shell script exec.sh:
#!/bin/bash
export CLASSPATH=$CLASSPATH:/pathto/hadoop-common-1.2.1.jar:\
/pathto/hadoop-core-1.2.1.jar:/pathto/exercise.class
hadoop jar exercise.jar exercise /data/input/inputfile.txt /data/output
This trows the ClassNotFoundException error :
Exception in thread "main" java.lang.ClassNotFoundException: exercise
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:425)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:274)
at org.apache.hadoop.util.RunJar.main(RunJar.java:153)
I realize the explicit path names might not be necessary but I've been a little desperate to double check everything. I've confirmed that in my exercise.java file exercise.class is in the job configuration via job.setJarByClass(exercise.class); and confirmed exercise.class is contained in exercise.jar. Can't seem to figure it out.
UPDATE
The exec.sh script with the full path of exercise.class. It's stored in my Eclipse project directory:
#!/bin/bash
export CLASSPATH=$CLASSPATH:/pathto/hadoop-common-1.2.1.jar:\
/pathto/hadoop-core-1.2.1.jar:/home/username/workspace/MVN_Hadoop/src/main/java.com.amend.hadoop.MapReduce/*
hadoop jar \
exercise.jar \
/home/username/workspace/MVN_Hadoop/src/main/java.com.amend.hadoop.MapReduce/exercise \
/data/input/inputfile.txt \
/data/output
When I actually try and run the exec.sh script using the explicitly written out path names, I also get a completely different set of errors:
Exception in thread "main" java.lang.ClassNotFoundException: /home/hdadmin/workspace/MVN_Hadoop/src/main/java/come/amend/hadoop/MapReduce/exercise
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:274)
at org.apache.hadoop.util.RunJar.main(RunJar.java:153)
I could see this possible errors.
From the Hadoop jar exercise.jar exercise /data/input/inputfile.txt /data/output please specify the full path of the exercise class. I.e org.name.package.exercise if exists. To cross check open the jar file and check the location of exercise.class location.
To continue, Hadoop doesn't expect jars to be included within the jars, since the path of Hadoop is set globally.
NEW:
See, the following path is some thing weird. "/home/hdadmin/workspace/MVN_Hadoop/src/main/java/come/amend/hadoop/MapReduce/exercise"
If you are running using your jar, how could a class path be so specific, instead of jar path. It could only be "come/amend/hadoop/MapReduce/exercise" this.

java - error while running compiled program with external jar

I have a simple jsoup test app in a single folder, in which there are 2 classes - LyricsGetter.java and Main.java - and a .jar file with jsoup library. When I compile the files with command javac -cp jsoup-1.8.2.jar LyricsGetter.java Main.java everything compiles ok, but when I try to run with java Main, I get:
Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/Jsoup
at LyricsGetter.getLyrics(LyricsGetter.java:16)
at Main.main(Main.java:14)
Caused by: java.lang.ClassNotFoundException: org.jsoup.Jsoup
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
On the other hand if I run with java -cp jsoup-1.8.2.jar Main, then I get Error: Could not find or load main class Main. So, what is the correct way to run this program?
You need to tell Java to look for classes in jsoup-1.8.2.jar, and the current folder (.).
On Windows, use:
java -cp jsoup-1.8.2.jar;. Main
or on Linux, OSX, or other Unix-like systems use:
java -cp jsoup-1.8.2.jar:. Main
(The difference is that the paths are separated by ; on Windows or : on *nix)

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.

SciTE: ClassNotFoundException in Java when using a newly added library (but not in command prompt)

I'm currently working with the Tidy class from the JTidy library provided by SourceForge (http://jtidy.sourceforge.net/).
I have already added the .jar library to the CLASSPATH, and I can compile the Java program in SciTE without any error. But when I try to run the program, SciTE prints out this output:
>java -cp . SourceViewer3
Exception in thread "main" java.lang.NoClassDefFoundError: org/w3c/tidy/Tidy
at SourceViewer3.main(SourceViewer3.java:65)
Caused by: java.lang.ClassNotFoundException: org.w3c.tidy.Tidy
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)
... 1 more
>Exit code: 1
Note: line 65 is where I call the constructor of the Tidy class,
Tidy tidy = new Tidy();
But when I try to run the program in command prompt, it runs smoothly without any problem. How can I fix the problem in SciTE?
Additional information: I'm using Windows 7 64 bit, SciTE 3.0.3 and Java version "1.7.0_02".
Nevermind, it is solved now.
Turns out that I must run it by using the command "java (java class name)" rather than using the default command "java -cp . (java class name)" used by SciTE, so that it doesn't override the setting of the CLASSPATH environment variable.
Sorry for the bother ^^;;

Categories