解决Maven项目中org.json.JSONObject冲突问题
在使用Maven构建项目时,可能会遇到 Found multiple occurrences of org.json.JSONObject on the class path 错误,这通常是由于项目中引入了多个包含 org.json.JSONObject 类的依赖库导致的。例如:
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 库和 json 库,而它们都包含 org.json.JSONObject 类。为了避免运行时行为不确定,需要排除其中一个依赖库。
排除android-json依赖
在Maven的 pom.xml 文件中,在相应的依赖项中添加排除 android-json 的配置,如下所示:
<dependency>
<groupId>com.example</groupId>
<artifactId>my-project</artifactId>
<version>1.0.0</version>
<exclusions>
<exclusion>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</exclusion>
</exclusions>
</dependency>
通过以上配置,将排除 android-json 的依赖,确保只有 json 库的 JSONObject 类被加载,从而确保可预测的运行时行为。
原文地址: https://www.cveoy.top/t/topic/oDt0 著作权归作者所有。请勿转载和采集!