Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding json jar to classpath java

I'm very new to Java world. I want to add the Json lib to compile my program. I downloaded the file from here.

When I tried to compile the program with

  javac -classpath json.jar MyClassName.java

I'm getting the error :

Note: JsonSimpleExample.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

And when I tried to run :

java MyClassName

I'm getting:

Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/JSONObject
    at MyClassName.main(MyClassName.java:9)
Caused by: java.lang.ClassNotFoundException: org.json.simple.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:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 1 more

Where I'm making the mistake?

like image 937
sriram Avatar asked Nov 18 '25 06:11

sriram


1 Answers

You have to use the -classpath when running with java as well.

java -classpath json.jar MyClassName
like image 53
Dan D. Avatar answered Nov 20 '25 19:11

Dan D.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!