0

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.

Jakob F
  • 1,046
  • 10
  • 23
Lack265
  • 1
  • 4
  • That error message is not saying that json-simple.jar was not found. It's saying that the class JSONObject was not found inside json-simple.jar. – k314159 Feb 12 '21 at 17:46
  • I have checked the .jar file and the class is there! i used " jar tf json-simple.jar" to check and it's there! – Lack265 Feb 12 '21 at 17:49

2 Answers2

1

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.

Queeg
  • 7,748
  • 1
  • 16
  • 42
  • yes in order for it to work i have to specify the classpath when running it also, other wise it just doesn't find the .jar file – Lack265 Feb 12 '21 at 19:39
0

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

Lack265
  • 1
  • 4