Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoSuchMethod error when using org.json to convert XML to JSON

Tags:

java

org.json

My project uses Spring Boot 2.0.4. I'm trying to read XML from a file and then convert it to JSON. This used to work, but recently it has stopped working and throws the exception below;

java.lang.NoSuchMethodError: org.json.JSONTokener.<init>(Ljava/io/Reader;)V
    at org.json.XMLTokener.<init>(XMLTokener.java:57) ~[json-20180813.jar:na]
    at org.json.XML.toJSONObject(XML.java:516) ~[json-20180813.jar:na]
    at org.json.XML.toJSONObject(XML.java:548) ~[json-20180813.jar:na]
    at org.json.XML.toJSONObject(XML.java:472) ~[json-20180813.jar:na]
    at com.zf.trw.visualisation.parser.handler.AttritionHandler.extractLineData(AttritionHandler.java:32) ~[classes/:na]
    at com.zf.trw.visualisation.parser.handler.HandlerImp.processFile(HandlerImp.java:79) ~[classes/:na]
    at com.zf.trw.visualisation.shared.service.ParserService.manuallyProcessAttritionData(ParserService.java:85) ~[classes/:na]
    at com.zf.trw.visualisation.parser.component.ScheduledTask.processAttritionDataFilesForAllLines(ScheduledTask.java:49) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_171]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_171]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_171]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_171]
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93) [spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_171]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_171]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_171]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [na:1.8.0_171]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_171]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_171]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_171]

The code I'm using to convert the XML to JSON is as follows;

String fileContents = FileUtils.readFileToString(file, "UTF-8");
json = XML.toJSONObject(fileContents);

In my POM file, I'm defining the dependency like this;

<dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20180813</version>
        </dependency>

The exception appears to show it's trying to use the same version that I have specified in the POM file, which is the latest version.

Why am I seeing this error?

like image 222
SheppardDigital Avatar asked Feb 13 '26 10:02

SheppardDigital


1 Answers

In my case this was because of dependency "spring-boot-starter-test" that had dependency on "android-json". After exclusion it worked:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>com.vaadin.external.google</groupId>
            <artifactId>android-json</artifactId>
        </exclusion>
    </exclusions>
</dependency>

I am using 20201115 version of json dependency.

like image 149
Piro Avatar answered Feb 18 '26 02:02

Piro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!