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

I'm trying to make a web-service with Tomcat and Eclipse using jersey library.
This is my service class:
package com.gontuseries.university;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
#Path("/university")
public class UniversityRestWs {
#GET
#Produces(MediaType.TEXT_HTML)
public String getHtmlUniversityInfo(){
return "<html><body>test</body></html>";
}
#GET
#Produces(MediaType.TEXT_PLAIN)
public String getTextUniversityInfo(){
return "test";
}
}
And this is file web.xml
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.gontuseries.university</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
When i test my service i get java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
I downloaded jersey from https://jersey.java.net/download.html
Can someone help me?
Thanks

For me the problem was that the server had no jars in the lib folder.
Make sure it has access to all dependencies in:
project -> properties -> development assembly -> add -> java build path entries.

We need to use provide build path to Eclipse. That must resolve issue.
Make sure it has access to all dependencies in:
project -> properties -> development assembly -> add -> java build path entries --> maven dependency
jersey bundle dependency
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.17</version>
<dependency>
If you are using maven, best few simple steps will work your application

Man, I had the same problem. I solve id adding the jersey-servlet-1.18.jar to the WEB-INF/lib.
Then, this folder saw like this:
Remember, add all this jars to yout build-path

Are you using Jersey 2.x? If so, the ServletContainer class has been moved to another package. This article has some good info on how to change your web.xml when migrating from Jersey 1.7 to 2.x:
https://java.net/projects/jersey/lists/users/archive/2013-06/message/91.

The com.sun.jersey.spi.container.servlet.ServletContainer class is contained in the "jersey-servlet.jar" so you should put it to WEB-INF/lib along with the "jersey-server.jar".

I got the same error -"java.lang.ClassNotFoundException : com.sun.jersey.spi.container.servlet.ServletContainer", while trying to execute restful jersey web service in tomcat from eclipse.
Following steps helped me to resolve the issue :
Correct the maven dependency to right version of dependent jars
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.19</version>
</dependency>
Right click on the eclipse project -> Maven -> Update Project
Ensure in eclipse properties development assembly is configured correctly. Right click on project - Properties -> development assembly -> Add -> java build path entries -> maven dependency

If you are using JERSEY-jaxrs-ri-2.9 version zip, Use the given below in WEB.XML file.
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
......
</init-param>

I was getting this error in an Eclipse project that had previously worked fine, running on Tomcat 7.0. Eventually I solved it by removing the project from Tomcat and adding it back again.

Above provided solutions are correct but even after making these changes i was getting this error. So what I did was removed all the maven downloaded Jersey jars and update Project again with new maven dependency and that resolved my issue

We get this error because of build path issue and outdated dependency names.Latest one's start from org.glassfish and not com.sun.After replacing with latest dependencies for 2.0, You should add "Server Runtime" libraries in Build Path.
"java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer"
Please follow below steps to resolve class not found exception.
Right click on project --> Build Path --> Build Path --> Add Library --> Server Runtime --> Apache Tomcat v7.0
Thanks,
Sachin G N

Related

jersey - ClassNotFoundException org.glassfish.jersey.servlet.ServletContainer

When I deploy my rest api with jersey to a tomcat 7.0 server I get an error that has been discussed on stackoverflow quite often but I haven't been able to resolve it:
java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1892)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1735)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:495)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:477)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:113)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1133)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1072)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5362)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5660)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1700)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1690)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Things I have checked:
Avoiding duplicate jersey versions
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey2.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey2.version}</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1</version>
</dependency>
So, ServletContainer is definitely imported
I notice however in the dependency hierarchy that the jersey servlet container is also imported through another library (dropwizard-core -> dropwizard-jersey -> jersey-container-servlet: 2.16) and the versions differ (my version is 2.25.1). But it says omitted for conflict with 2.25.1
Where is Apache Tomcat v7.0 package library
My project was originally a pure Maven project and I added the Dynamic Web Project facet lateron. When I compare to a test project where I successfully tested a jersey rest api, I don't see the Apache Tomcat v7.0 libraries on my build path. In my test project I see
versus
WebContent/WEB-INF/lib is empty
As mentioned in this question the jersey libraries should be deployed to my WEB-INF/lib/ folder. I did Project -> Deployment Assembly -> Add -> Java Build Path Entries -> Maven Dependencies -> Finish. But as soon as I do Project -> Maven -> Update the step above is reset (i.e. I don't see Maven Dependencies in the Web Deployment Assembly anymore. Despite this is strange, in the successful test jersey project the lib folder is also empty.
EDIT
There is something more fundamental going wrong. In the successful jersey rest api, I got the subfolder structure
target
+---- generated-sources
+---- JerseyRestDemo-0.0.1-SNAPSHOT
+---- m2e-wtp
+---------- web-resources
+----------------- META-INF
+-----------------------maven
+---- maven-archiver
+--------- pom.properties
+---- test-classes
WebContent
+---- META-INF
+--------- MANIFEST.MF
+---- WEB-INF
+--------- lib
+----------web.xml
However, in my current project I just see
target
+----- generated-sources
+---------- annotations
+----- maven-status
+---------- maven-compiler-plugin
+--------------- compile
WebContent
+---- META-INF
+--------- MANIFEST.MF
+---- WEB-INF
+--------- lib
+----------web.xml
Thanks for your help
I converted the project again from maven to webapp following this post and then it worked

What version of Jersey should I use with Glassfish 3.1 and Java 7?

After I added JAX-RS 2.1/Jersey 2.26+ libraries to my dynamic web project I got the next error:
Glassfish error after upload WAR file 1
The server is running Glassfish 3.1 with JDK 1.7.0_80
I tried to use Jersey 1.19.1 ZIP bundle but then Glassfish said:
Glassfish error after upload WAR file 2
The WAR file with the latest Jersey runs perfect with Tomcat 9 and Java 8 but I need this project to run with Glassfish 3.1 and Java 7.
If you can provide a link to the correct version of Jersey I would appreciate!
This is my very fisrt question so excuse any errors.
To run Jersey with Glassfish 3 you really need to exclude all Jersey.2 things from you war and either:
use the bundled Jersey.1 implementation of your Glassfish 3.1.2.2 installation (check the /lib or /modules folders you shall see it, or online documentation)
EDIT: Jersey version should be 1.11.1
<!-- Keep 1.11.1 for Jersey which is Fish's version -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.11.1</version>
<!-- <scope>provided</scope> ... set Provided scope for GF3 deployment -->
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.11.1</version>
<!-- <scope>provided</scope> ... set Provided scope for GF3 deployment -->
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.11.1</version>
<!-- <scope>provided</scope> ... set Provided scope for GF3 deployment -->
</dependency>
<!-- Etc. - Organize your dependencies accordingly ... -->
replace the bundled Jersey.1 libraries of your Glassfish3 installation by another Jersey.1 implementation/libs(if you need a fresher version) - but never expect Jersey.2 to run with GF3.
Nonetheless you can actually write code that works with Jersey.1 and Jersey.2 but when you come to packaging and deployment you must build it with appropriate target JVM, dependencies and deployment descriptors - as soon as your modularization is "ok". I didn't read this post fully personnally but why not have a look here.
From Jersey latest (2.26) documentation
https://jersey.github.io/documentation/latest/modules-and-dependencies.html#d0e560
Since Jersey 2.26, all modules are build using Java SE 8 and there is no support for running it on older Java SE distributions.
So if you really want to use jersey 2.26 +, JDK should be 1.8+. No alternatives.

JAXWS Client ServiceConfigurationError on weblogic

I have created a client to connect to external webservice. This was done by using wsimport to generate the artifacts and then copied the java classes in my existing web application.
I have tomcat on my workstation and this projects deploys and functions perfectly in this environment.
But as soon as I deploy the same to weblogic servers I get the below error.
Can you please help me understand the problem and its potential solution.
java.util.ServiceConfigurationError: javax.xml.ws.spi.Provider: Provider weblogic.wsee.jaxws.spi.WLSProvider could not be instantiated: java.lang.ClassCastException
at java.util.ServiceLoader.fail(ServiceLoader.java:207)
at java.util.ServiceLoader.access$100(ServiceLoader.java:164)
at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:353)
at java.util.ServiceLoader$1.next(ServiceLoader.java:421)
at javax.xml.ws.spi.Provider.getProviderUsingServiceLoader(Provider.java:180)
at javax.xml.ws.spi.Provider.provider(Provider.java:140)
at javax.xml.ws.Service.<init>(Service.java:92)
Thanks in advance!
In my case importing jaxb - api did the trick
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>...</version>
</dependency>
I changed the version of jaxws-api.jar library and it worked. I was using 2.2.5 and I changed it to 2.1-1 version and it worked for me.
Also, in the META-INF/weblogic-application.xml of your EAR, put
<?xml version="1.0" encoding="UTF-8"?> <weblogic-application xmlns="http://www.bea.com/ns/weblogic/90">
<application-param>
<param-name>webapp.encoding.default</param-name>
<param-value>UTF-8</param-value>
</application-param>
<prefer-application-packages>
<package-name>org.springframework.*</package-name>
<package-name>javax.jws.*</package-name>
<package-name>javax.wsdl.*</package-name>
<package-name>com.ctc.wstx.*</package-name>
<package-name>javax.xml.ws.*</package-name>
<package-name>com.sun.xml.messaging.saaj.*</package-name>
</prefer-application-packages>

Calling JSP files gives a NoSuchMethodError when app deployed outside of Eclipse

I have a maven project that works completely as intended inside of eclipse. It also builds and runs outside of eclipse, but when I try to call the frontend (JSP web pages) then I get the following:
Problem accessing /. Reason:
javax.servlet.ServletContext.getJspConfigDescriptor()Ljavax/servlet/descriptor/JspConfigDescriptor;
Caused by:
java.lang.NoSuchMethodError:
javax.servlet.ServletContext.getJspConfigDescriptor()Ljavax/servlet/descriptor/JspConfigDescriptor;
...
I've looked around and it seems that this message is associated with an incompatibility between Servlet 2.5 and Servlet 3.0. But I already have Servlet 3.0 as a dependency in my pom:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
So I can't figure out why this dependency that I need isn't being included when I build and deploy outside of eclipse even though the build itself is successful.
Any idea what could be causing this and how to fix it?
EDIT:
The JSP access isn't configured in web.xml. The index.jsp file is the welcome file set for the jetty server with this snippet:
// configure webapp
WebAppContext webroot = new WebAppContext();
webroot.setResourceBase("src/main/webapp");
webroot.setContextPath("/");
webroot.setWelcomeFiles(new String[] {"index.jsp"});
webroot.setParentLoaderPriority(true);
server.setHandler(webroot);
The remaining few jsp files are in the webapp folder.
EDIT 2:
I've examined the contents of the jar created when packaging my project and it appears that there are multiple copies of the javax/servlet/Servlet.class in the jar. This is a bit perplexing. I'm assuming that these other dependencies (listed below) that I have in my pom must be adding the additional Servlet.class files.
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>8.1.8.v20121106</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>8.1.8.v20121106</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jsp</artifactId>
<version>8.1.8.v20121106</version>
</dependency>
But I'm not sure how to fix any of this...
If anyone has any ideas, I'd love to hear them. The only real constraints that I have is that I have to use jetty 8.1.8.v20121106.
It seems to me you might be deploying a servlet 3.0 on tomcat 6 which is not supported and would cause this exception.
An additional possibility is that your exporting additional .classes within your war that are interfering with tomcat's default libraries.

ClassNotFoundException while loading ContextLoaderListener

I am working on a webapp, using spring 3.0, hibernate. When I try to deploy my app on WAS 7.0, it gives me the error - Failed to load listener: org.springframework.web.context.ContextLoaderListener]: java.lang.ClassNotFoundException:
Here is how my web app looks:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ABC</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
The exception being thrown is as follows,
com.ibm.ws.webcontainer.webapp.WebApp logError SRVE0293E: [Servlet Error]-[Failed to load listener: org.springframework.web.context.ContextLoaderListener]: java.lang.ClassNotFoundException: class java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
at java.beans.Beans.instantiate(Beans.java:190)
at java.beans.Beans.instantiate(Beans.java:75)
at com.ibm.ws.webcontainer.webapp.WebApp.loadListener(WebApp.java:1643)
at com.ibm.ws.webcontainer.webapp.WebAppImpl.loadListener(WebAppImpl.java:671)
at com.ibm.ws.webcontainer.webapp.WebApp.loadLifecycleListeners(WebApp.java:1554)
So, is there anything wrong in web.xml ?
Edit: Sorry I did not mention, I am using Maven to get the jars. I have the required jar file in the WEB-INF folder too i.e. org.springframework.web.context
Have a look at the following link
http://forum.springsource.org/showthread.php?60812-ClassNotFoundException-org.springframework.web.con-text.ContextLoaderListener
It says you can fix this problem by going to project properties -> Deployment Assembly and adding the Maven Dependency Build Path entry
class java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
at java.beans.Beans.instantiate(Beans.java:190)
Is the .jar file containing org.springframework.web.context.ContextLoaderListener on the classpath?
You have the wrong definition of the spring-web dependency, instead of:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
You should have what I've written in the comments:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
ArtifactId is spring-web. ${spring.version} of course is not important, just be sure it matches your defined version string.
Take any jars that you add to your project and make sure that they're also placed in your WEB-INF/lib directory. This is where your server looks at runtime when 3rd party libs are referenced. They aren't put there automatically; but there are ways you can automate this (ie using an ANT script or something like that). Just to get things up and running though, you can just manually copy/paste jars to that directory. If you add them to that directory outside of your IDE, make sure you refresh the folder from within your IDE after placing the files there.
the java engine fails too find the class (as your exception says: java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener)
java will search for the class on the class-path. you have a folder in your web app structure (on the computer where your 'was'-server runs) where you can put jar-files containing classes. you need to put spring.jar or spring-web.jar in this folder. i'm guessing the folder is called lib, and it will probably be located in a folder called WEB-INF, but i'm not sure, cause i don't know 'was'.
after you've put the jar-file in there, you'll probably need to restart your web/app-server. hope this helps! ^^
Project> properties> deployment assembly> add > referenced project class path entries> maven dependencies
Deploy again.
It works for me
Dave is right! You need all the required JARs in two places:
If you are seeing exceptions when you start the server, then you do not have the required JARs in WEB-INF/lib dir, so you need to keep all the JARs there.
If you are seeing any compile errors in your Java code, then you do not have your build path configured correctly. Keep all the JARs you put in WEB-INF/lib in your build path as "Referenced Libraries".
struggled with this same error all day ...
i had the spring-web jar, but turns out that i also needed the spring-context-support jar.
i added it into my pom and everything is working now.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework-version}</version>
</dependency>
error log:
com.ibm.ws.webcontainer.annotation.WASAnnotationHelper collectClasses unable to instantiate class
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
SRVE0293E: [Servlet Error]-[Failed to load listener: org.springframework.web.context.ContextLoaderListener]:
java.lang.ClassNotFoundException: class java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
loadLifecycleListeners SRVE0279E: Error occured while processing global listeners for the application {0}: {1}
java.lang.NullPointerException
at com.ibm.ws.webcontainer.annotation.WASAnnotationHelper.inject(WASAnnotationHelper.java:266)
I had the same exact problem.
Hopefully this helps someone.
Some of my dependency jars were missing from my WEB-INF/lib folder but were there in Referenced libraries.
I had to to do a maven clean install again to make sure all the jars were pulled into the lib folder.
The problem might be in your pom.xml too if it is not able to pull in the jars properly. So keep an eye out for that as well.
Project-> Maven clean and build -> lib folder refresh and make sure all jars are there (or the particular jar that throws the exception)

Categories

Resources