在 Java 项目开发中,经常会遇到 'Found multiple occurrences of org.json.JSONObject on the class path' 的错误。这通常是因为项目中引入了多个包含相同类路径的依赖,例如:

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

为了确保程序的正常运行,需要解决这种冲突。

解决方法:

排除 android-json 依赖:

在项目的 pom.xml 文件中添加以下代码,排除 android-json 依赖:

<dependency>
    <groupId>com.vaadin.external.google</groupId>
    <artifactId>android-json</artifactId>
    <version>0.0.20131108.vaadin1</version>
    <exclusions>
        <exclusion>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
        </exclusion>
    </exclusions>
</dependency>

这段代码会从 android-json 依赖中排除 org.json 依赖,确保程序在运行时使用相同的 org.json 版本,从而解决冲突并保证可预测的运行时行为。

解决 Java 项目中 org.json.JSONObject 冲突问题

原文地址: https://www.cveoy.top/t/topic/oDtX 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录