简体   繁体   中英

jar file not recognized in eclipse

I needed a JSON API for Java and this seemed to be the most popular:

https://github.com/douglascrockford/JSON-java

I downloaded the zip from github and the directory structure looks like this:

$ ls
CDL.java          JSONArray.java      JSONString.java   README
Cookie.java       JSONException.java  JSONTokener.java  XML.java
CookieList.java   JSONML.java         JSONWriter.java   XMLTokener.java
HTTP.java         JSONObject.java     Kim.java          zip
HTTPTokener.java  JSONStringer.java   Property.java

I want to use this JSON API in my java 8 project in eclipse so I create a jar out of it:

$ jar cf json-java.jar *.java zip

Then I right clicked on project, selected build path > Configure Build Path > Add External Jars and then select the json-java.jar created above.

Problem is when I try to use the following in the project:

import org.json.JSONObject;

It gives me the error "The import org.json cannot be resolved", so I look in "Referenced Libraries and I notice the json-java.jar doesn't contain any class files or anything. It seems that it didn't import the jar properly:

在此处输入图片说明

Any idea what I might be doing wrong?

Download the ZIP file from here . and extract it to get the Jar.

To add this Jar to your build path,

Right click the Project > Build Path > Configure build path > Select Libraries tab > Click Add External Libraries > Select the Jar file Download

What you did was, put all the source files into a jar file.

That is not gonna work. You need to compile the java into class files and then put those in your jar file.

Alternatively, you can download the binary jar from the following location and use that instead.

http://mvnrepository.com/artifact/org.json/json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM