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 OverflowAdd 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.
java - org.json not found in jar with Maven - Stack Overflow
maven - java.lang.ClassNotFoundException: org.json.JSONException - Stack Overflow
java - Maven + Spring Boot: Found multiple occurrences of org.json.JSONObject on the class path: - Stack Overflow
maven - org.json.simple.JSONObject VS org.json.JSONObject , JSONException cannot be resolved to a type - Stack Overflow
you can solve this add the following dependency (http://mvnrepository.com/artifact/org.json/json/20080701)
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20080701</version>
</dependency>
Remove the JSONException and replace it with a normal Exception. This solved the issue for me. The cause of the error is a little complicated and related to jar optimization in some packages in the new versions.
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.
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
Have you considered using Maven? for instance, if you wanted a JAR to pars JSON you could just include...
http://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-asl/1.9.6
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.6</version>
</dependency>
in a pom.xml, this will give you the jar for JSON.
Or better yet, use the org.json one...
http://mvnrepository.com/artifact/org.json/json
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
I downloaded a jar from here: http://code.google.com/p/org-json-java/downloads/detail?name=org.json-20120521.jar&can=2&q=
Probably your simple json.jar file isn't in your classpath.
I was facing same issue in my Spring Integration project. I added below JSON dependencies in pom.xml file. It works for me.
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
A list of versions can be found here: https://mvnrepository.com/artifact/org.json/json
You will need to either throw that exception or catch and handle it.
For example, catching the exception would mean:
try{
JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
}catch(JSONException e){
// Recovery
}
Throwing the exception would mean:
private void doSomething() throws JSONException {
JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
}
Unhandled exception: org.json.JSONException means it is prone to JSONException . Android Studio is asking you to handle this possible exception by,
try {
JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
} catch (JSONException ignored) {
ignored.printStackTrace(); //now it is in log
}
This will fix the problem
Thanks