java.lang.ClassNotFoundException: org.json.JSONException
Asked Answered
E

3

5

I am building a simple maven project. I used build path to add json as a third party library and also added it to web-inf library. In the end, i added the dependency in pom.xml. However, with all the effort above, i still get this exception. Any information and advice is appreciated!

below is the exception

SEVERE: Servlet.service() for servlet [sfmserver] in context with path [/mysfmovies] threw exception [Servlet execution threw an exception] with root cause
java.lang.ClassNotFoundException: org.json.JSONException

Below is the content of my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mysfmovies</groupId>
  <artifactId>mysfmovies</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>mysfmovies Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
      <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
    </dependency>
            <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20090211</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.google.code.geocoder-java</groupId>
            <artifactId>geocoder-java</artifactId>
            <version>0.16</version>
            <scope>provided</scope>
        </dependency>
  </dependencies>
  <build>
    <finalName>mysfmovies</finalName>
  </build>
</project>
Efflux answered 25/12, 2014 at 3:55 Comment(6)
Can you append contents of pom.xml to the question.Princely
<dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20090211</version> <scope>provided</scope> </dependency>Efflux
@nash_ag, sorry, i am new to the stackoverflow, still figuring out how it works. let me know if you can see my pom.xmlEfflux
I can see the snippet, did you add it under dependencies and rebuilt via mvnPrincely
I add the whole pom.xml to my question. Yes, i think so, i use maven build with clean and install. is this what you mean ? @nash_agEfflux
@nash_ag,do you need another information of my project to locate the problem?Efflux
A
6

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>
Antirrhinum answered 17/2, 2015 at 20:18 Comment(1)
Did not fix for meHilten
S
1

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.

Subtle answered 9/8, 2021 at 9:20 Comment(1)
Can you provide an example of a normal exception that handles JSONExceptionHilten
I
0

You have put all the dependencies in provided scope. That means that maven war plugin won't put them into WEB-INF/lib directory and thus they won't be on the classpath when you run your web app. You really should have put them into compile scope (that's the default one, just remove the tag).

If you still really want them provided, put them into your container's lib directory, or check the endorsedDirs configuration of the war plugin.

Indefinable answered 26/12, 2014 at 10:8 Comment(1)
I haved the same problem with JHipster, when run the war wit not spring boot, this dependencies was not resolved.Iceland

© 2022 - 2024 — McMap. All rights reserved.