This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

package org.json does not exist

 
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
 
Marshal
Posts: 67435
257
  • 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.
 
author
Posts: 23853
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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.


 
Rancher
Posts: 4450
47
  • 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... ;)
 
I wasn't selected to go to mars. This tiny ad got in ahead of me:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!