1

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

If I have already set the $CLASSPATH, why the Java Compiler is telling me that the library doesn't exist, when indeed it exists?

ivanleoncz
  • 1,643
  • 6
  • 19
  • 32

1 Answers1

1

When compiling, you must specify the absolute path to the .jar file which contains the package with its classes, like this:

javac -cp /usr/share/java/json-20160212.jar JsonParser.java

According to the official documentation from Oracle about PATH and CLASSPATH variables:

The preferred way to specify the class path is by using the -cp command line switch. This allows the CLASSPATH to be set individually for each application without affecting other applications. Setting the CLASSPATH can be tricky and should be performed with care.

The default value of the class path is ".", meaning that only the current directory is searched. Specifying either the CLASSPATH variable or the -cp command line switch overrides this value.

ivanleoncz
  • 1,643
  • 6
  • 19
  • 32