0

Before you mark it as DUPLICATE please understand I've checked all the relevant links but to no avail.

I'm calling the following Service.

@Path("/courses")
public class CourseService {

//  @GET
//  @Produces(MediaType.TEXT_PLAIN)
//  public String hello(){
//      return "Hello World !!!";
//  }

    private DataService _service;

    public CourseService(String jsonData) throws ParseException, IOException, JSONException {
        _service = new DataService(jsonData);
    }

    @GET
    @Produces(MediaType.APPLICATION_XML)
    public ArrayList<Course> getAllCourses(){
        return _service.get_handler().getAllCourses();

    }
}

If I just run the commented portion of the code my application runs fine and produces "Hello World"

But then when I run my actual service it gives me the following error.

type Exception report

message Servlet.init() for servlet Jersey Web Application threw exception

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet.init() for servlet Jersey Web Application threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1502)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1458)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)
root cause

java.lang.NoClassDefFoundError: org/json/JSONException
    java.lang.Class.getDeclaredConstructors0(Native Method)
    java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
    java.lang.Class.getConstructors(Class.java:1651)
    org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.<init>(MethodHandler.java:265)
    org.glassfish.jersey.server.model.MethodHandler.create(MethodHandler.java:155)
    org.glassfish.jersey.server.model.ResourceMethod$Builder.createInvocable(ResourceMethod.java:550)
    org.glassfish.jersey.server.model.ResourceMethod$Builder.build(ResourceMethod.java:536)
    org.glassfish.jersey.server.model.Resource$Builder.processMethodBuilders(Resource.java:663)
    org.glassfish.jersey.server.model.Resource$Builder.buildResourceData(Resource.java:599)
    org.glassfish.jersey.server.model.Resource$Builder.build(Resource.java:655)
    org.glassfish.jersey.server.model.Resource.from(Resource.java:798)
    org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:465)
    org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:184)
    org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:350)
    org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:347)
    org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    org.glassfish.jersey.internal.Errors.processWithException(Errors.java:255)
    org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:347)
    org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:392)
    org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:177)
    org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:369)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1502)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1458)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)

This is the structure of my Project Directory

enter image description here I've tested my CourseService using a TestClient and it works fine. What am I doing wrong.

Zeus
  • 2,213
  • 8
  • 28
  • 45
  • 2
    *NoClassDefFoundError: org/json/JSONException* Do you have that library http://mvnrepository.com/artifact/org.json/json? – Tunaki Apr 01 '16 at 16:07
  • 1
    Unsure how exception message itself and duplicates are unclear. Please elaborate. You have still nowhere mentioned how you made explicitly sure that you have that class in runtime classpath. – BalusC Apr 01 '16 at 16:11
  • @Tunaki java-son.jar is added as an external jar to the build path. – Zeus Apr 01 '16 at 16:16
  • I'm not sure how to make maven include it. – Zeus Apr 01 '16 at 16:16
  • the eclipse build path and a maven dependency are totally different things. Make sure you add the library as a maven dependency – f1sh Apr 01 '16 at 16:18
  • It needs to go to WEB-INF/lib directory of Tomcat assuming that you are using tomcat from what I see in stacktrace. If you use maven add it as a dependency in your pom.xml file. – randominstanceOfLivingThing Apr 01 '16 at 16:18
  • org.json json 20140107 – randominstanceOfLivingThing Apr 01 '16 at 16:19
  • @SureshKoya I'm really not familiar with Maven so this might sound naive. So I've added the dependency as you've specified in the pom.xml. do I also need to explicitly put the jar in the WEB_INF/lib ? – Zeus Apr 01 '16 at 16:25
  • My WEB-INF doesn't have a lib folder, do I have to create it and put it there? – Zeus Apr 01 '16 at 16:26
  • @SureshKoya do I have to add all the classes inside the package to my Maven dependency. – Zeus Apr 01 '16 at 16:35
  • Check the contents of your target directory. You will have a war file which you are putting in Tomcat. – randominstanceOfLivingThing Apr 01 '16 at 16:55

2 Answers2

0

You shouldn't be defining dependencies like that. All your dependencies should be managed with something like maven or gradle. That being said, did you add it the jar to the class path?

Since your using maven, you would want to add this to your <dependencies> section. For example:

<dependencies>
    <!-- other dependencies -->
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <!-- or w.e. version you wanted to use. -->
        <!-- See http://mvnrepository.com/artifact/org.json/json-->
        <version>20160212</version> 
     </dependency>
     <!-- other dependencies -->
</dependencies>

Eclipse should automatically add dependencies like the one above to your class path.

Check out one of the official tutorials on maven: https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

  • My project is an existing maven project. – Zeus Apr 01 '16 at 18:05
  • Ah I didn't notice the pom and kinda assumed you weren't using it since your manually managing your jars... So that means you should be able to add the dependency to your pom.xml to resolve this problem. – Javier Velasquez Apr 01 '16 at 20:49
-1

java.lang.NoClassDefFoundError: org/json/JSONException

Please make sure JSONException is defined and accessible.

Keshav
  • 29
  • 4
  • It is a part of an external jar java-son.jar which I've added to the build path in eclipse. – Zeus Apr 01 '16 at 16:15
  • Please try to put that jar in maven dependency. As on server local jar files do not work. You can include it using pom.xml. Please go through this link for maven dependency process: http://stackoverflow.com/questions/4955635/how-to-add-local-jar-files-in-maven-project – Keshav Apr 01 '16 at 16:34