Lack265
Lack265

Reputation: 1

json-simple.jar not found

I'm currently developing a project that needs a json creator class in java. I am trying to use the json-simple library, and have already downloaded and copied the .jar file to my projects directory. I am compiling my java file using

javac -cp json-simple.jar report.java

however when i try to run it i get this error message:

Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/JSONObject
    at report.main(report.java:22)
Caused by: java.lang.ClassNotFoundException: org.json.simple.JSONObject
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 1 more

I am using ubuntu and cannot use an IDE like eclipse, can anyone help me? thank you in advance.

Upvotes: 0

Views: 717

Answers (2)

queeg
queeg

Reputation: 9384

You defined the classpath while compiling your code. Did you also specify the classpath when running your code?

Java does not link the classes on the classpath into your code, it just references it so the jars have to be on the classpath during runtime also.

Upvotes: 1

Lack265
Lack265

Reputation: 1

ok guys I have discovered the solution! For anyone that eventually meets the same problem the solution is simple, after compiling the program dont run it like the other java files, you need to give the command -cp to say the directory of the .jar file, in this example it looks something like this:

java -cp json-simple.jar: report.java

Upvotes: 0

Related Questions