在Eclipse中使用org.json.JSONObject [英] Using org.json.JSONObject in Eclipse

查看:1385
本文介绍了在Eclipse中使用org.json.JSONObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Eclipse中为Java应用程序使用 JSONObject 。我在每个论坛都搜索过。我没有找到正确的答案。而且,他们不断提到一个我不能找到的 WEB-INF / lib目录

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.

我尝试在项目.jar文件中添加 json-lib-2.2.2-jdk15.jar 。这似乎也不起作用。它显示此错误 -

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-


导入org.json.JSONObject无法解析

Import org.json.JSONObject cannot be resolved

如何解决这个问题?

推荐答案

问题有点不清楚,所以我的答案是一般的...

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

为了在Java中使用第三方库(如您的案例中使用JSON-lib),库必须在编译执行期间存在>(运行时)程序。这是通过下载 .jar 文件,并告诉Java在哪里找到它。 Java使用 classpath 的概念:

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是一个参数–在命令行中设置,或者通过环境变量–它告诉Java虚拟机或编译器在哪里查找用户定义的类和包。

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.

(从维基百科

您提到的错误导入org.json.JSONObject无法解析)意味着编译期间 .jar 不在类路径上。

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

要在Eclipse中添加第三方库,请将 .jar 文件存储在项目的某个位置(专用 lib 文件夹将是一个不错的选择),然后右键单击您的项目,选择Properties→ Java构建路径→库,单击添加JAR ...,然后选择您的 .jar 文件。从现在开始,Eclipse将在编译期间将其添加到类路径。

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.

在执行期间将其添加到类路径取决于您正在运行的项目类型。如果您从Eclipse运行简单的 main 方法,则 .jar 将自动添加到类路径。如果您在Tomcat,JBoss等容器中运行Web应用程序,那么将 .jar 添加到类路径的标准方法将是将其放入您部署的应用程序的 WEB-INF / lib 目录。有关Web应用程序及其相关的更多信息,请查看 Java EE教程部署。

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.

这篇关于在Eclipse中使用org.json.JSONObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆