Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Thursday, May 18, 2023

How to fix java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer? [Solution]

The error message clearly says that the Java runtime is not able to find a class called ServletContainer for the Jersey library. This is the Servlet class you have specified in the deployment descriptor of your application. It's similar to DispatcherServlet of Spring MVC and this error is similar to the error you get when DisplayServlet was not in the classpath.
Anyway, java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer comes when you are trying to use Jersey but have not added required dependency on your classpath e.g. those JAR files which contain the class "com.sun.jersey.spi.container.servlet.ServletContainer".

Btw, if you are new to Servlet and JSP then I also suggest you go through a comprehensive course to learn Servlet and JSP in depth. If you need a recommendation then I suggest you check out these Servlet and JSP online courses, it will help you to learn Servlet and JSP from your home or office. 

What is ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer? Analysis 

Jersey is an open-source Java library, which allows you to write RESTful Web Services in Java. Jersey 2.x implements JAX-RS 2.0 API and Jersey 2.17 is the most recent release of Jersey, but "java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer" is related to Jersey 1.x because in Jersey 2.x this class is moved into another package org.glassfish.jersey. So if you have configured your web.xml like shown here

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
id="WebApp_ID" version="2.5">
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-name>Jersey REST Service</servlet-name>

but doesn't have Jersey 1.x dependency then you will get this error.

Cause of com.sun.jersey.spi.container.servlet.ServletContainer

The error message says "com.sun.jersey.spi.container.servlet.ServletContainer" class is not available in the classpath.  Now you need to understand two things, first on which JAR file this class is present and second what is your classpath.

If you are facing this problem in a Java web application e.g. in Tomcat, you must remember that dependency JARs are loaded by the WebappClassLoader from the WEB-INF/lib directory.

So to solve this error, you simply download Jersey 1.19 JAR bundle and put that in your Java web application's WEB-INF directory. Jersey 1.19 JAR bundle is a single-JAR Jersey bundle to avoid the dependency management of multiple Jersey module JARs and can be downloaded from

If you are using Maven you can solve this error by including the following dependency in your pom.xml file. When you build a WAR file using Maven it will automatically copy all dependencies in the WEB-INF/lib folder.

Another cause of this problem is that incorrect dependency (JAR files) in ClassPath. Some developer includes Jersey 2 JAR files and puzzled why Java is still complaining about "com.sun.jersey.spi.container.servlet.ServletContainer", well there is a significant difference between Jersey 1x and Jersey 2.x in terms of this class. In Jersey 2 this class is moved from "com.sun.jersey" package to "org.glassfish.jersey" package. So even if you have downloaded Jersey 2 JAR file you will still get this error.

If you are not sure how to find which JAR a particular class belongs to, you can always use the search type shortcut Ctrl + T to find the JAR file provided you are using Eclipse. I am sure other IDE like Netbeans and IntelliJ also has a similar shortcut, any Netbeans or IDEA lover here to help?

If you are not sure where to download a JAR file, go to the Maven

I am using Jersey 1.17 and it appears that as far back as 1.12, the Servlet module is refactored into its own jar (outside jersey-server.jar). This new jar is called jersey-servlet.jar.

com.sun.jersey.spi.container.servlet.ServletContainer Solution

As you are using com.sun.jersey.spi.container.servlet.ServletContainer in your web.xml as your servlet, you need to include jersey-servlet.jar in your dependencies. This can be in the form of Maven dependencies to it will get packaged inside the war file.

Eclipse and Tomcat

If you are trying to make a Web application to provide web service using Jersey library in Tomcat and Eclipse and still getting a problem after adding jersey-server.jar in the classpath. You can follow these steps to add a dependency on deployment assembly

Project -> properties -> development assembly -> add -> java build path entries -> maven dependency

How to fix java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer Jersey

That's all about what causes the com.sun.jersey.spi.container.servlet.ServletContainer error and how to solve com.sun.jersey.spi.container.servlet.ServletContainererror in Java web application. As I explained, there could be two reasons, incorrect classpath, where you have not added jersey-servlet.jar in the classpath, or you are using jersey 2.x JAR files but have configured web.xml for Jersey 1.x

Related Java troubleshooting guides for your reference:
  • How to connect to MySQL database from Java Program [steps]
  • General Guide to solve java.lang.ClassNotFoundException in Java [guide]
  • How to solve java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver in Java? [solution]
  • How to solve java.lang.UnsatisfiedLinkError: no ocijdbc11 in Java [solution]
  • How to fix java.lang.ClassNotFoundException: org.postgresql.Driver error in Java? [solution]
  • How to solve 
  • java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory? [solution]
  • How to solve java.lang.classnotfoundexception sun.jdbc.odbc.jdbcodbcdriver [solution]
  • Too many files open [solution]
  • Failed to read from SocketChannel: Connection reset by peer [fix]
  • Exception in thread "main" java.lang.ExceptionInInitializerError in Java Program [fix]
  • 2 ways to solve Unsupported major.minor version 51.0 error in Java [solutions]
  • Fixing Unsupported major.minor version 52.0 Error in Java [solution]
  • java.lang.ClassNotFoundException: [solution
  • Map failed and java.lang.OutOfMemoryError: Map failed  [fix]
  • Cannot assign requested address: JVM_Bind [fix]
  • org.hibernate.MappingException: Unknown entity Exception in Java [solution]
  • Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 [solution]
  • java.lang.ClassNotFoundException:org.Springframework.Web.Context.ContextLoaderListener [solution]
  • How to solve java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Java MySQL? [solution]
Thanks for reading this article so far. If you find my solution useful, please share this article with your friends and colleagues. 

1 comment :

Unknown said...

Thank you soo much. i worked for me

Post a Comment