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:
Answer from ivanleoncz on serverfault.comThe 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.
Videos
Bit late, but I wanted to share my opinion on this.
I faced this problem recently when I found a Java project with both libraries and they were used at the same time.
I think that org.json is easier to read and to use, for 2 main reasons (for my needs):
JsonObject is immutable. You can't add new key/value pairs to an already existing JsonObject (reference here: javax.json: Add new JsonNumber to existing JsonObject)
It takes a few lines to pretty print a JsonObject or JsonArray, while it only takes 1 line to do it with JSONObject or JSONArray. Example:
CopyStringWriter sw = new StringWriter(); Map<String, Object> properties = new HashMap<>(); properties.put(JsonGenerator.PRETTY_PRINTING, true); JsonWriterFactory writerFactory = Json.createWriterFactory(properties); JsonWriter jsonWriter = writerFactory.createWriter(sw); jsonWriter.writeObject(jsonObject); //JsonObject created before jsonWriter.close(); String prettyPrintedJSON = sw.toString();
That is the code I use to get an indented JSON to write to a file. And with org.json I only need jsonObject.toString(4).
Another difference is the constructors. You will need a JsonObjectBuilder to create a JSON with javax.json. One step more that can be avoided.
I'm sure there are more differences (not sure if it's possible to create a JsonObject from a String) but these are my thoughts.
JSONObject, as mentioned, is provided by android's API. JsonObject is specifically used for Java EE development which is essentially for web applications and networking capabilities among other things.
The reason Android does not prepackage JsonObject from Oracle Java EE package is because alot of the things javax can do, are not allowed within android like accessing the internet without permission. This means importing the entire jars files of javax would conflict with Android.
If you plan to build your own backend with Java EE, I would highly suggest using JsonObject over JSONObject. On the other hand, if you know a prebuilt rest service or something similar that supports Android's JSON even better.
Hi. I have a recurring maven(?) issue that I hope is not unique to me and has been solved by someone somewhere.
As JSON parser, I use JSON-Java (the one with package org.json), instead of more famous ones, as the DX and API feel more fit for most/all my projects.
However, from time to time, I reach a very dreadful situation, where the "version" of the JSON-Java library that is available to my code is "not" the one that I have declared in my pom.xml file. In once case, the copyright notice in the source that I could see by clicking the class name in VSCode was from 2010, with the painful difference to the modern version that all parsing methods threw checked exceptions. In another instance, the JSONArray class did not implement Iterable/Iterator where in modern versions it does.
This is likely a maven transitive dependency issue, but the reason it is so visible for this particular library, is because either many libraries already have their own dependency on it, or that it's interface has evolved quite significantly along the way. Likely both.
The solution "in the book" for this is apparently to do "mvn dependency:tree" and exclude JSON-Java explicitly from other dependencies that depend on it. But it doesn't work for me! In my dependency three, only the recent version that is in my own pom file is shown, whereas in code/IDE (VSCode + IntelliJ), I can only use the old version. My deployment involves building a fat Jar, so it happens there too.
Am I doing something wrong? Is there a proven way to make only a certain version of a dependency available to my code, regardless of other versions that may be present deeper in the class path? Does the order of dependencies in pom file matter? and how can I strictly control the versions of dependencies that appear in my fat jar, in case it is possible at all?
Many thanks