Using org.json.JSONObject in Eclipse

瘦欲@ 提交于 2019-12-04 20:12:20
Eli Acherkan

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!