Add json jar to your classpath
or use java -classpath json.jar ClassName
Or add this to your maven pom.xml depedencies:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
Answer from Alya'a Gamal on Stack OverflowVideos
Add json jar to your classpath
or use java -classpath json.jar ClassName
Or add this to your maven pom.xml depedencies:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
As of today (15th July 2020), you need to use latest maven repository as per below:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20200518</version>
</dependency>
For any new version in the future, you can check it here:
- https://mvnrepository.com/artifact/org.json/json
Then simply replace 20200518 with latest new version value.
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
Add under
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
The following exclusion:
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</exclusion>
</exclusions>
Similarly, for Gradle projects:
testCompile("org.springframework.boot:spring-boot-starter-test") {
exclude group: "com.vaadin.external.google", module:"android-json"
}
Background:
org.json works great, but has a license clause that some people don't like ("The Software shall be used for Good, not Evil."). So Vaadin wanted to use the library, but couldn't be sure they wouldn't use it for evil someday. Instead, they re-implemented the interface, published android-json and used it as a drop in replacement for org.json. Others began to use android-json as well so that they too would not be bound by the requirement of not using their software for evil.
This is a fine solution, except that when the two libraries are on the classpath, they collide.
Solution:
If you get this error from conflicting transitive dependencies, then your best bet is to exclude either Vaadin's android-json library (brought in by Spring), or exclude the org.json library (brought in by another dependency). Vaadin's version is meant to be an identical implementation, but there are subtle differences.
If you're using org.json in your code and it is conflicting with Spring's Vaadin dependency, then I would recommend trying open-json. It's a port of Vaadin's re-implementation of org.json, but they changed the packages so you won't have any conflicts with org.json:json or com.vaadin.external.google:android-json
https://github.com/openjson/openjson
Add gradle dependency:
implementation('com.github.openjson:openjson:1.0.12')
Or in Maven:
<dependency>
<groupId>com.github.openjson</groupId>
<artifactId>openjson</artifactId>
<version>1.0.12</version>
</dependency>
Then update any imports that were being used by org.json classes.