SOLVED: java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer

If you have start working on Jersey then you might face this issue while configuring it. This is the result of insufficient project dependencies on runtime. This issue is likely to be faced in tomcat server.

Random exceptions

Random exceptions

The error log will look like this.

SEVERE: Servlet /JerseyHelloWorld threw load() exception
java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1713)
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1558)
	at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:527)
	at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:509)
	at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:137)
	at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)
	at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
	at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5033)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5317)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
	at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
	at java.util.concurrent.FutureTask.run(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)

To solve this issue, correct the project dependencies in pom.xml like below. If you are not using maven for project dependencies, include the corresponding jar files in classpath.

 <dependency>
	<groupId>com.sun.jersey</groupId>
	<artifactId>jersey-server</artifactId>
	<version>1.17.1</version>
</dependency>
<dependency>
	<groupId>com.sun.jersey</groupId>
	<artifactId>jersey-core</artifactId>
	<version>1.17.1</version>
</dependency>
<dependency>
	<groupId>com.sun.jersey</groupId>
	<artifactId>jersey-servlet</artifactId>
	<version>1.17.1</version>
</dependency>

This will solve the issue.

Happy Learning !!

Comments

  1. sagar says

    i want change my maven project into dynamic web project.I deleted pom.xml file and i took one new work space and i import the project. i getting following errors.i added all jar files required.
    Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener.
    Exception sending context destroyed event to listener instance of class org.springframework.web.context.ContextLoaderListener
    java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory.

  2. Naresh says

    I’m still facing the classNotFoundException, though i have all the required jars specified above exists in my project library.

    SEVERE: Servlet /SimpleRest threw load() exception
    java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)

    I’m using tomcat 7 + JDK 7 + REST 2.0 API

    Can you please advise me. Thanks

  3. PNR says

    This is really very helpful. It solved my errors. I’ve also started reading your other articles. They are very simple and straight forward.

    Thanks again!!!!

  4. Pop says

    It doesn’t solve the issue for me :(
    I tried everything I found on the internet, but it’s like none of them actually does anything.
    I still have tons of errors like:
    java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer

      • Pop says

        Thank you, Lokesh! I solved the problem in the meanwhile and I wanted to post it here for everyone. In my case, all I had to do (and it’s kinda funny) is restart my computer. Why? I’m guessing this could be a Netbeans bug – maybe it doesn’t close Tomcat even when you press stop? The point is, I added the dependencies like in your post and it still didn’t work, even after stopping and starting again both my web service and Tomcat. Then, the next day, without doing anything else but restarting my computer, everything was working well.

        • says

          Really weird, but thank you for updating your fix here. It might be helpful for other’s working on netbeans. [I am wondering if others facing similar issue would have to wait till next day to restart the computer.. :) just kidding.]

  5. chris says

    Wow. Thanks a million. All the tutorials have older versions of jersey and none of them told me to use jersey-servlet. I fought with this for HOURS. thanks,

Leave a Reply

Your email address will not be published. Required fields are marked *


5 × = forty