0

I added a jar file to my project by following these steps:

Right click on your project
Select Build Path
Click on Configure Build Path
Click on Libraries and select Add External JARs
Select the jar file from the required folder
Click and Apply and Ok

This imports my jar file just fine, and the Java compiler can detect references to the new library just fine.

When I run the application though, it crashes, saying it doesn't recognize the library:

Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/JSONObject
    at com.ebook.Main.main(Main.java:14)
Caused by: java.lang.ClassNotFoundException: org.json.simple.JSONObject
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 1 more

What am I doing wrong when importing this jar (json-simple)?

Vismark Juarez
  • 613
  • 4
  • 14
  • 21
  • you need to add -classpath arg to java command – Sharon Ben Asher Sep 15 '20 at 05:09
  • @SharonBenAsher what value should be passed along with this flag? I'm reading the docs an they mention `class search path of directories and zip/jar files`, but I'm not sure what that means. – Vismark Juarez Sep 15 '20 at 05:16
  • java -cp : Note that ":" is a seperator. Use ";" if you are in windows. For e.g. java -cp /Users/kk/eclipse-workspace/JsonSampleProject/json-simple-1.1.1.jar:. sample.json.SimpleJson – Kaushal Sep 15 '20 at 06:11

2 Answers2

0

If you have added the jar files properly then you should check in your com.ebook.Main.main(Main.java:14) class whether the related import statement is proper or not.after that clean and rebuild.Hope this will solve your problem.

TanvirChowdhury
  • 2,498
  • 23
  • 28
0

When you are running the jar file, if you are using library's you need to setup the classpath:
java -cp C:\foo\bar\JsonLibrary.jar your_jar_file.jar
this answer might help you setup your classpath.

Note: The reason why it worked in Eclipse, is because Eclipse automatically handles the classpath for you, but after compiling you need handle it yourself.

hms11
  • 54
  • 6