1

I am trying to use JSONObject for a Java application in Eclipse. I have searched everywhere and in every forum. I haven't found a proper answer. Moreover, they keep mentioning a WEB-INF/lib directory, which I am not able to find.

I tried adding the json-lib-2.2.2-jdk15.jar in the project .jar files. This didn't seem to work either. It shows this error-

Import org.json.JSONObject cannot be resolved

How can I solve this problem?

1 Answer 1

7

The question is a bit unclear, so my answer is equally general...

In order to use a third-party library in Java (like JSON-lib in your case), the library must be present during compilation and during execution (runtime) of your program. This is done by downloading the .jar file and telling Java where to find it. Java uses the concept of classpath:

Classpath is a parameter – set either on the command-line, or through an environment variable – that tells the Java Virtual Machine or the compiler where to look for user-defined classes and packages.

(from Wikipedia)

The error you mention ("Import org.json.JSONObject cannot be resolved") means that the .jar wasn't on the classpath during compilation.

To add a third-party library in Eclipse, store the .jar file somewhere in your project (a dedicated lib folder would be a good choice), then right-click on your project, choose Properties → Java Build Path → Libraries, click "Add JARs..." and choose your .jar file. From now on, Eclipse will add it to the classpath during compilation.

Adding it to the classpath during execution depends on the kind of project you're running. If you have a simple main method which you're running from Eclipse, the .jar will be automatically added to the classpath. If you're running a web application in a container such as Tomcat, JBoss etc., then the standard way to add the .jar to the classpath would be to put it in the WEB-INF/lib directory of your deployed application. Have a look at the Java EE tutorial for more information on web applications and their deployment.

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

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.