[Solved] java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer

We can face this exception if we are setting up jersey 2 project first time in eclipse using the Maven plugin.

The error message is simple enough to identify the root cause that the Jersey libraries are not in classpath.

The Error

The exception will look like this.

SEVERE: Servlet /JerseyDemos threw load() exception
java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
	at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:532)
	at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:514)
	at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:133)
	at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1137)
	at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1081)
	at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5027)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)
	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.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)

Solution – Add Jersey Library in Deployment Assembly

Open your project’s deployment assembly configuration.

Eclipse Deployment Assembly
Eclipse Deployment Assembly

Add Build path jar files in assembly so that they can be added to lib folder in the final .war file.

Deployment Assembly - Add Build Path Entries
Deployment Assembly – Add Build Path Entries

The updated assembly will look like this.

Deployment Assembly - Updated
Deployment Assembly – Updated

Now when you again run the project after building it, this will run fine and will be able to find org.glassfish.jersey.servlet.ServletContainer class.

Happy Learning !!

Leave a Reply

17 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.

Our Blogs

REST API Tutorial