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
I've included the Maven based project into my application, added Deployment Assembly which shows that the .jar file gets copied to WEB-INF/lib/ImportedProject.jar.
Problem occurs when imported lib wants to reference the lib that it is using which, apparently, does not get included inside the ImportedProject.jar.
Example:
ImportedProject pom.xml has:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
When I add ImportedProject.jar to my Non-Maven project and run it, I get exception:
threw exception [Servlet execution threw an exception] with root cause
java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
My question is: How to add both that Maven project and its dependencies?
maven dependency plugin helps to get required project library with it's dependencies.
So, If you would like to add POM based project on NON-POM based project, all you would do is to execute mvn dependency:copy-dependencies command and you will have all dependencies on target/dependencies folder. Copy all jar from dependencies and paste to WEB-INF/lib directory.
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>
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
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.