5

I use JSON simple to parse JSON and I get NoClassDefFoundError when trying to create JSONParser object.

import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
...
JSONParser parser = new JSONParser();

I compile program with command:

javac MyProgram.java -cp json-simple-1.1.1.jar

And it compiles fine. But when I execute program with this command:

java MyProgram

I get NoClassDefFoundError

What am I doing wrong?

EDIT:
Full error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/parser/JSONParser
        at getNotesFromNoter.sendPost(getNotesFromNoter.java:53)
        at getNotesFromNoter.main(getNotesFromNoter.java:14)
Caused by: java.lang.ClassNotFoundException: org.json.simple.parser.JSONParser
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        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)
        ... 2 more
1
  • you are missing the jar in your classpath. Try using an IDE like Eclipse, configure the build path and run it inside it Commented Jun 5, 2014 at 16:53

3 Answers 3

6

You're not including the Simple JSON jar file in your classpath when running. You want:

// Unix
java -cp .:json-simple-1.1.1.jar MyProgram

// Windows
java -cp .;json-simple-1.1.1.jar MyProgram

(The : or ; is the path separator for the relevant operating system.)

When you compile Java and specify a classpath, that's just telling the compiler about the classes to compile against - it doesn't include the library in the result of the compilation, so you still need to specify the classpath in order to run the code, too.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried to run code with classpath but I still get the same error
@SPython: Well you've not told us the full error, which makes it harder to help you. Maybe there are more jar files you need. Please edit your question with more information.
1

If you are using an IDE, download the jar from this link.In netbeans and eclipse you can add this jar by right clicking on project then selecting properties->libraries->Add jar->Select the jar and click ok.

Comments

1

Did you compile your code with -cp json-simple-1.1.1.jar?

Sometime ago, I made a Test.java, and compiled with:

javac -cp json-simple-1.1.1.jar Test.java

Then:

java -cp .:json-simple-1.1.1.jar Test

And It ran perfectly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.