• Post Reply Bookmark Topic Watch Topic
  • New Topic

package org.json does not exist  RSS feed

 
Ivan Leon
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys! My first post here!

I downloaded the JSON library for Java from Maven Repo and installed the .JAR file at the Java library directory on UBuntu 14.04.
I tried compile a simple JAVA snippet for JSON parsing, but the compiler (javac) is telling me that the "org.json" library does not exist..

Here are some details:

$ echo $CLASSPATH
/usr/share/java

$ ll /usr/share/java/json*
-rw-r--r-- 1 root root 49176 Jun 12 22:31 /usr/share/java/json-20160212.jar

$ jar tf /usr/share/java/json-20160212.jar
META-INF/MANIFEST.MF
META-INF/
META-INF/maven/
META-INF/maven/org.json/
META-INF/maven/org.json/json/
META-INF/maven/org.json/json/pom.properties
META-INF/maven/org.json/json/pom.xml
org/
org/json/
org/json/CDL.class
org/json/Cookie.class
org/json/CookieList.class
org/json/HTTP.class
org/json/HTTPTokener.class
org/json/JSONArray.class
org/json/JSONException.class
org/json/JSONML.class
org/json/JSONObject$1.class
org/json/JSONObject$Null.class
org/json/JSONObject.class
org/json/JSONString.class
org/json/JSONStringer.class
org/json/JSONTokener.class
org/json/JSONWriter.class
org/json/Property.class
org/json/XML.class
org/json/XMLTokener.class

$ javac JsonParser.java
JsonParser.java:1: error: package org.json does not exist
import org.json.*;
^
1 error


** Please, forgive my ignorance but.. what is missing? I came from Python, where the import and code execution are pretty straightforward.

Thank you so much!

Kind Regards
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Is that a system CLASSPATH or a user CLASSPATH or did you set it specifically for this session? You should only set a CLASSPATH for the session with the -cp option or similar.
That CLASSPATH looks wrong, anyway; it should have the name of the .jar in, and should also include the current directory. Try
export CLASSPATH=.:/usr/share/java/json-20160212.jar
to set that CLASSPATH for the lifetime of the current terminal. Read more about the CLASSPATH in the Java™ Tutorials. I am not sure about the setting that I gave you.
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Leon wrote:
$ echo $CLASSPATH
/usr/share/java


That classpath only specifies class files whose root is in that directory. To specify the jar file, you either have to explicitly have the jar file in the classpath, or use the "*" specifier to mention that the directory is for jar files.

Henry
 
Ivan Leon
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got no words. Both answers helped me.

I specified the .JAR and IT WORKED: $ javac -cp /usr/share/java/json-20160212.jar JsonParser.java

Thank you, guys!
Thank you, Code Ranch!

------------------------------------------------------
A brief (parentheses) here, just for reflection.

* In my opinion, Java Compiler could be more objective, not so trick, about this sort of situation.
Look at Google, and you will find thousands of Java developers (from beginners to not so beginners), having problems with this..
Thinking about Python: how many issues can you find in Google about module import, in comparison with Java.
For me that means something: maybe the approach of Java about compiling, could be revisited.


 
Dave Tolls
Rancher
Posts: 2914
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know about python etc, but with Java dependencies are usually deployed with their app and not in a central area on a machine.
So for a web app, you have a lib directory which holds all the jar files used by that web app.
This means you don't pollute the machine with multiple versions of a jar file, possibly resulting in versioning issues between apps.

So, sticking that jar file in a common place would give me pause for thought, to say the least.
 
Ivan Leon
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes sense.

I'm not a Python expert but, I believe that for this approach, in order to keep specific libraries and their versions for a web app, it must be used the Virtual Env, which performs a kind of "chroot" for your application, totally isolating the app from the rest of the system.

I do believe that in Python, importing modules and libraries is much easier and straightforward, but maybe Java is more organized and correct about keeping the libraries in just one place, avoiding issues regarding library versions... ;)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!