92

When I run mvn test I get this warning. How can I fix it?

Found multiple occurrences of org.json.JSONObject on the class path:

        jar:file:/C:/Users/Chloe/.m2/repository/org/json/json/20140107/json-20140107.jar!/org/json/JSONObject.class
        jar:file:/C:/Users/Chloe/.m2/repository/com/vaadin/external/google/android-json/0.0.20131108.vaadin1/android-json-0.0.20131108.vaadin1.jar!/org/json/JSONObject.class

You may wish to exclude one of them to ensure predictable runtime behavior

Here is my pom.xml. The only reference to JSON is

    <!-- https://mvnrepository.com/artifact/org.json/json -->
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
    </dependency>

Apache Maven 3.5.3

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
Chloe
  • 25,162
  • 40
  • 190
  • 357
  • 1
    You may want to drop your dependency to `org.json` because it is considered as non-free software. [Spring Boot removed their dependency to it because of this](https://github.com/spring-projects/spring-boot/issues/5929) but this sometimes results in conflicts like yours. – Didier L Nov 26 '19 at 16:49

6 Answers6

165

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"
}
ryenus
  • 15,711
  • 5
  • 56
  • 63
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • Has this issue been reported to the Spring Boot project? I can find [a similar ticket for Spring Cloud](https://github.com/spring-cloud/spring-cloud-deployer-kubernetes/issues/142) but nothing for Spring Boot. It seems strange that they (transitively) depend on a library that repackages `org.json:json`. – Didier L Nov 25 '19 at 16:16
  • @DidierL related https://github.com/spring-projects/spring-boot/issues/15967 resolved as *don't want to make any changes to the starter.* – Ori Marko Nov 26 '19 at 06:16
  • 4
    Thanks! I dived a bit more into it and in fact it goes much deeper than that. In fact [Spring Boot removed dependencies to `org.json`](https://github.com/spring-projects/spring-boot/issues/5929) because [it is considered as a non-free license due to the "not evil usage" clause](https://wiki.debian.org/qa.debian.org/jsonevil). This is also causing [some trouble with the module path](https://github.com/skyscreamer/JSONassert/issues/116) apparently. – Didier L Nov 26 '19 at 11:08
  • 1
    Classpath clashes even when using spring-boot-starter.. how unfortunate! – Nico Van Belle Apr 30 '20 at 14:45
  • Thanks for showing how it's done for Gradle as well! – Clawdidr Aug 18 '20 at 01:00
  • Thank you so much for showing the gradle version!!! – patrickw May 12 '21 at 18:17
  • This worked for me on Spring Boot 2.6.3 with Gradle 7.3.3, but I had to delete the cached file for the warning to go away. – Asa Feb 12 '22 at 23:23
34

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.

Planky
  • 3,185
  • 3
  • 29
  • 39
4

Add the below line for gradle projects.

testCompile('org.springframework.boot:spring-boot-starter-test'){
        exclude group: "com.vaadin.external.google", module:"android-json"
}
Parthiban
  • 527
  • 2
  • 13
  • 24
3

This worked for me:

configurations {
     testImplementation.exclude group: 'com.vaadin.external.google', module: 'android-json'
}
Alex Stanovsky
  • 1,286
  • 1
  • 13
  • 28
1

Gradle kotlin DSL version based on the accepted answer

testImplementation("org.springframework.boot:spring-boot-starter-test") {
        exclude (
                group = "com.vaadin.external.google",
                module = "android-json"
        )
    }
xilef
  • 2,199
  • 22
  • 16
1

You can exclude android-json module from testImplementation.

testImplementation('org.springframework.boot:spring-boot-starter-test') {

         exclude group: "com.vaadin.external.google", module:"android-json"
    
}
Catarina Ferreira
  • 1,824
  • 5
  • 17
  • 26
ihernandez
  • 11
  • 3