Problems in creating JSF project in eclipse [duplicate] - eclipse

I'm using this :
Tomcat 7.0
JSF 2.0
JRE 7
but when trying to run my application, I got the following exception:
java.lang.IllegalStateException: Could not find backup for factory javax.faces.application.ApplicationFactory.
at javax.faces.FactoryFinder$FactoryManager.getFactory(FactoryFinder.java:1011)
at javax.faces.FactoryFinder.getFactory(FactoryFinder.java:343)
at org.apache.myfaces.context.servlet.FacesContextImplBase.getApplication(FacesContextImplBase.java:159)
at org.apache.myfaces.context.servlet.FacesContextImplBase.getELContext(FacesContextImplBase.java:210)
at javax.faces.component.UIViewRoot.setLocale(UIViewRoot.java:1463)
at org.apache.myfaces.webapp.AbstractFacesInitializer._createFacesContext(AbstractFacesInitializer.java:477)
at org.apache.myfaces.webapp.AbstractFacesInitializer.initStartupFacesContext(AbstractFacesInitializer.java:449)
at org.apache.myfaces.webapp.StartupServletContextListener.contextInitialized(StartupServletContextListener.java:113)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4797)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5291)
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)
Any ideas why?
Thanks,

That may happen if your webapp's runtime classpath is polluted with multiple JSF impls/versions. The org.apache.myfaces entries in the stack trace tells that you're using MyFaces. This problem thus suggests that you've another JSF implementation like Mojarra in the webapp's runtime classpath which is conflicting with it. It's recognizable by jsf-api.jar, or jsf-impl.jar, or javax.faces.jar. If you remove all of them, then this problem should disappear.
Or, if you actually intented to use Mojarra instead of MyFaces (you did namely not explicitly state the intented JSF impl/version anywhere in your question, but you just generically stated the JSF spec as in "JSF 2.0", so perhaps you actually had no clue what you was all doing), then you should be removing myfaces-*.jar files from your webapp.
See also:
JSF wiki page - Installing JSF
How to properly install and configure JSF libraries via Maven?
Difference between Mojarra and MyFaces
JSF implementations and component libraries

Complementing BalusC's answer, I recently got this error when trying to run an independent JAR with a Spring Boot application that has JSF as front-end with Spring-managed beans. Switching the packaging from JAR to WAR solved the problem.

For me {Tomcat 8, JSF 2.2, JRE 8}, the following steps worked:
Download JSTL jars API && IMPL and place it in your Tomcat lib.
Since downloading Majorra directly from eclipse in project
facets configuration is recently impossible ; "Zip File is empty Exception",
manually download jsf-api.jar and jsf-impl.jar and include them in a
new user library added to build path(only once!).
This is my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>TestJSF</display-name>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>

In addition to what's already said, I faced this issue when importing a third party project, the reason was that two profiles were defined in the pom.xml, one for My Faces and another for Mojarra.
If this is your case, you just need to select one of them with maven -P or in Project properties, Maven section, indicating the profile(s) to be used.

In my case this exception was caused on webapp shutdown by the presence of com.sun.faces.config.ConfigureListener in web.xml, running Mojarra 2.2.x under Tomcat 8.0. As described in this answer, this causes Tomcat to register that listener twice (both automatically and by virtue of the web.xml entry) and hence, among other things, its contextDestroyed method is executed twice, the second time producing the described exception (most probably because JSF was already shut down by the first listener registration).
I had no classpath conflict in my case: just one version of Mojarra (2.2.6) and no MyFaces.

Related

IllegalArgumentException: The servlets named [Hello] and [com.crunchify.jsp.servlet.HelloCrunchify] are both mapped to the url-pattern [duplicate]

This question already has answers here:
java.lang.IllegalArgumentException: The servlets named [X] and [Y] are both mapped to the url-pattern [/url] which is not permitted
(7 answers)
Closed 6 years ago.
I have created a dynamic web project in eclipse as mentioned in this doc: sampledoc
When I run the program in server, I get this error in the console:
Caused by: java.lang.IllegalArgumentException: The servlets named [Hello] and [com.crunchify.jsp.servlet.HelloCrunchify] are both mapped to the url-pattern [/CrunchifyServlet] which is not permitted
at org.apache.tomcat.util.descriptor.web.WebXml.addServletMapping(WebXml.java:308)
at org.apache.catalina.startup.ContextConfig.processAnnotationWebServlet(ContextConfig.java:2373)
at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2055)
at org.apache.catalina.startup.ContextConfig.processAnnotationsWebResource(ContextConfig.java:1940)
at org.apache.catalina.startup.ContextConfig.processAnnotationsWebResource(ContextConfig.java:1934)
at org.apache.catalina.startup.ContextConfig.processAnnotationsWebResource(ContextConfig.java:1934)
at org.apache.catalina.startup.ContextConfig.processAnnotationsWebResource(ContextConfig.java:1934)
at org.apache.catalina.startup.ContextConfig.processAnnotationsWebResource(ContextConfig.java:1934)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1147)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:779)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:306)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:95)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5150)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
... 6 more
I tried deleting the server from the Servers tab and adding again. Did a project clean. Nothing seems to solve the problem.
My web.xml looks like this:
<?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_3_0.xsd"
version="3.0">
<display-name>CrunchifyJSPServletExample</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>com.crunchify.jsp.servlet.HelloCrunchify</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/CrunchifyServlet</url-pattern>
</servlet-mapping>
I am using Tomcat 8 and my java home is set to "javac 1.8.0_05"
.
Please help!!!
I think the key part of that stack trace is this:
The servlets named [Hello] and [com.crunchify.jsp.servlet.HelloCrunchify] are both mapped to the url-pattern [/CrunchifyServlet] which is not permitted
You will either need to remove one of these servlets or deconflict the url-patterns. Do you have another application that is mapped to the same url-pattern?
You have omitted some relevant information, your other servlet mappings.
The error tells it all:
Caused by: java.lang.IllegalArgumentException: The servlets named [Hello] and [com.crunchify.jsp.servlet.HelloCrunchify] are both mapped to the url-pattern [/CrunchifyServlet] which is not permitted
You have two servlet mappings, mapped to the same URI.
Try changing the URI for the /CrunchifyServlet to just /Crunchify
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>com.crunchify.jsp.servlet.HelloCrunchify</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Crunchify</url-pattern>
</servlet-mapping>
Try This one :
Go to
C:\Program Files\Apache Software Foundation\Apache Tomcat 8.x.xx\bin
find
catalina.bat
copy and paste it into another drive as windows will not let you edit here.
Now open the file with any editor.
Find noJuliConfig and noJuliManager, you will reach somewhere like this image
Before Editing
You can clearly see one set JAVA_OTPS="Something"for noJuliConfig and noJuliManager.
Now all you have to do is to remove the double quotes, an the edited part will look like thisAfter Editing
Now just replace the original catalina.bat with this edited one.
Restart your IDE.
and you're done.
You can thank me later for that. :D

java.lang.IllegalArgumentException: The main resource set specified [...] is not valid

I'm having trouble starting my Tomcat server, it used to work, but I did something wrong and now it throws me this exception:
Caused by: java.lang.IllegalArgumentException: The main resource set specified [E:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\workspace\j2eeapplication\target\j2eeapplication-0.0.1-SNAPSHOT] is not valid
at org.apache.catalina.webresources.StandardRoot.startInternal(StandardRoot.java:643)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 9 more
And this is my web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>J2EE Application Example</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>facelets.DEVELOPMENT</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
<param-value>1</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>Resources Servlet</servlet-name>
<servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Resources Servlet</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<filter>
<filter-name>charEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>charEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
I looked at different solutions over the forums, but nothing worked. Final option will be uninstalling tomcat and fresh installation, cause I read that might work. Thanks for the help in advance.
Seems like you have an outdated web application referenced in your Tomcat embeded server (You are using Tomcat As within Eclipse right?).
First checkout the deployed application within you server, and check the artifact name j2eeapplication-0.0.1-SNAPSHOT and version. You may need to remove it and clean your working directory the redeploy it and you should be safe.
For me, this was caused by a file permission issue. We use a different deployment strategy where I work (not something I can change) which means the webapp exists in a completely different directory to the normal Tomcat directory structure. The above exception occurred when the Tomcat runtime didn't have permission to access that directory.
I had this error when I was starting an application that was designed for Tomcat 8 using Tomcat 7.
A maven update did the trick for me.
Right click on your maven project:
Maven > Update Project...
Select All
Check "Force Update of Snapshots/Releases"
Click Ok
Now, right click on your server:
Clean...
Just in case this might help anyone who come later, I managed to start my tomcat8 server after close and reopen the front project in:
The main resource set specified [project-to-be-reopened]
In my case, it's maven dependency that's causing me this issue, updating maven-dependency will also help.
I had a similar issue. Just if someone else runs into this problem:
For me it was caused because I had an old project deployed, then closed that project. For several weeks, everything was fine, until I used the "Clean..." command of eclipse on that tomcat server. From that point on the famous The main resource set specified [...path to deployment location of this project...] is not valid was raised every time I tried to start Tomcat.
What solved this problem for me was just removing that old project from tomcat (Right click on that entry under Tomcat Server and choose "Remove").
I had a similar issue with one of the projects. I tried all the solutions to this question. None worked for me. I then double-clicked the Tomcat server to see the actual config. In there, under the tab "Modules", there is a list of projects currently associated with the Tomcat server. I could see that the "trouble-causing project" was listed there. I clicked on that and hit remove.
Then everything started to work just fine.
In my case with Tomcat 9 and using Eclipse under Windows, I somehow removed the application from the webapp directory under the Tomcat true server and could no longer start the standalone Tomcat server service. Got the same root cause text as the initial posted question. My solution was to copy the Eclipse version of the application (from the workspace's .metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps tree) to the C:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps directory.
Note that I am not using Maven in my workspace, and it was just a standard Java Web project.
For me, the cause was another:
In .metadata.plugins\org.eclipse.wst.server.core\servers.xml the "server" element had no "list" element.
I added the "list" element:
<server auto-publish-setting="1" auto-publish-time="1" configuration-id="/Servers/Tomcat v9.0 Server 9091 ASM-config" deployDir="wtpwebapps" hostname="localhost" id="Tomcat v9.0 Server 9091 ASM" jrebel.old-auto-publish-setting="2" name="Tomcat v9.0 Server 9091 ASM" runtime-id="Apache Tomcat v9.0" server-type="org.eclipse.jst.server.tomcat.90" server-type-id="org.eclipse.jst.server.tomcat.90" start-timeout="9000" stop-timeout="30" testEnvironment="true" timestamp="4">
<list key="modules" value0="adsuite-market::org.eclipse.jst.j2ee.server:adsuite-market::jst.web::2.4"/>
and restarted Eclipse. Then it worked as normal again.

Removing #WebServlet causes java.lang.ClassNotFoundException

I'm using Tomcat 7 in eclipse Juno with servlet 3.0 specs (jdk 1.7).
When I create a new servlet using Eclipse IDE it automaticaly create a new mapping using #WebServlet("/foo") statement and everything works fine (the servlet works).
Removing the #WebServlet("/foo") mapping and using the manual one in web.xml:
<web-app>
<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-path>foo.Servlet</servlet-path>
</servlet>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/foo</url-pattern>
</servlet-mapping>
</web-app>
causes Tomcat crash:
SEVERE: Allocate exception for servlet java.lang.ClassNotFoundException:
I'm sure that foo.Servlet.Servlet1 is the correct path and name.
I've to manually compile the servlet before starts Tomcat? I run the project directly from eclipse ide, setting up a Tomcat 7 Runtime Environment.
Your Servlet declaration in web.xml is not correct, You need to change
<servlet-path>foo.Servlet</servlet-path>
with
<servlet-class>foo.Servlet</servlet-class>
Also you should add the schema declaration in your web.xml file, it would have saved you all this trouble by showing the error in your xml editor because there is nothing such as <servlet-path> in it.
<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">

web.xml servlet tag breaks down with Tomcat6 on EC2

ENV: Amazon Linux with Tomcat6 on EC2.
I create a simple dynamic web project with an index.jsp on Eclipse, load it to cloud and it shows up correctly. But once I create a servlet and do the same process, the page says service not available and gives error code 503.
I have tried to remove the servlet tags in web.xml, then it works again.
Just to clarify, I have two existing dynamic web projects that I'd like to have deployed on cloud. Both are running fine on local Tomcat6.
edit:
<?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" 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>Test</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>Se</display-name>
<servlet-name>Se</servlet-name>
<servlet-class>Servlet.Se</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Se</servlet-name>
<url-pattern>/Se</url-pattern>
</servlet-mapping>
</web-app>
Above is my web.xml, if I remove servlet,servlet-mapping blocks, it works again.
The UnsupportedClassVersionError usually happens because you are using a newer JDK version for compiling than the version used at runtime. Try compiling with the same JDK version used by Tomcat.
SEVERE: Error deploying web application archive Test.war
java.lang.UnsupportedClassVersionError: Servlet/Se : Unsupported major.minor version 51.0 (unable to load class Servlet.Se)
This type of error occurs when you have compiled your running your application on a different version of JRE than it was originally intended for or compiled on.
51.0 means that it is expecting JRE version 7 and (it is most probable that) you must be running it on JRE 6 on your ec2 server. First determine what is the version of the JRE on your ec2 server with the command
$ java -version
and make sure that is matches the one with on which the code was initially compiled on. It is most probable that you compiled your code on JRE7 and on ec2 server, JRE6 is configured and you might want to upgrade it on ec2 server, or downgrade on your machine.

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

I am trying to build a simple hello world application for two days using Jersey + Google app engine. For simple AppEngine project I followed these tutorials and both works just fine
https://developers.google.com/appengine/docs/java/gettingstarted/creating
https://developers.google.com/appengine/docs/java/webtoolsplatform
But now I am trying to add Jersey and following this tutorial http://www.vogella.com/articles/REST/article.html.
But server keeps giving me
java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
when I add these lines in web.xml:
<?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>TestServer</display-name>
<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.test.myproject</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
I have downloaded Jersey JAX-RS 2.1 RI bundle from here and have added all jar files in WEB-INF/lib folder as described in tutorial. And even after two days nothing is working. I have searched several times on Google and apparently people who are using Maven have solved it somehow but I am not using Maven neither did the guy who wrote that tutorial.
Just to check if even com.sun.jersey.spi.container.servlet.ServletContainer exists in imported Jersey jars I tried to just write this fully qualified name in Java and let the intellisense finish names but I couldn't get any intellisense after com.sun.je so my last guess is that there have been some package rearrangement in latest Jersey build and jersey is no longer inside com.sun. I am exhausted and I would appreciate any kind of help.
You have downloaded Jersey 2 (which RI of JAX-RS 2). The tutorial you're referring to uses Jersey 1. Download Jersey 1.17.1 from (here), should be sufficient for you.
Jersey 1 uses com.sun.jersey, and Jersey 2 uses org.glassfish.jersey hence the exception.
Also note that also init-param starting with com.sun.jersey won't be recognized by Jersey 2.
Edit
Registering Resources and Providers in Jersey 2 contains additional info on how to register classes/instances in Jersey 2.
If you are using jersey 2.x then you need different configuration in web.xml as servlet class is change in it. you can update your web.xml with following configuration.
<servlet>
<servlet-name>myrest</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>your.package.path</param-value>
</init-param>
<init-param>
<param-name>unit:WidgetPU</param-name>
<param-value>persistence/widget</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myrest</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Add this in pom
<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>
It's an eclipse setup issue, not a Jersey issue.
From this thread ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
Right click your eclipse project Properties -> Deployment Assembly -> Add -> Java Build Path Entries -> Gradle Dependencies -> Finish.
So Eclipse wasn't using the Gradle dependencies when Apache was starting .
I also faced a similar issue. Resolved the problem by going through the step step tutorial from the below link.
http://examples.javacodegeeks.com/enterprise-java/rest/jersey/jersey-hello-world-example/
The main thing to notice is that the jersey libraries should be placed correctly in TOMCAT WEB-INF/lib folder. It is done automatically by the Eclipse settings mentioned in the above link. It will create a WAR file with the dependent JAR Files. Else, you will run into problems with ClassNotFound Exception.
apache-tomcat-7.0.56-windows-x64\apache
-tomcat-7.0.56\webapps\JerseyJSONExample\WEB-INF\lib
"11/23/2014 12:06 AM 130,458 jersey-client-1.9.jar
11/23/2014 12:06 AM 458,739 jersey-core-1.9.jar
11/23/2014 12:06 AM 147,952 jersey-json-1.9.jar
11/23/2014 12:06 AM 713,089 jersey-server-1.9.jar"
4 File(s) 1,450,238 bytes
The second tutorial explains about how to create a Webservice which produces and consumes JSON output.
http://examples.javacodegeeks.com/enterprise-java/rest/jersey/json-example-with-jersey-jackson/
Both the links gave a good picture on how things work and save a lot of time.
try this :
org.glassfish.jersey.servlet.ServletContainer
on servlet-class
I had the same problem as you though I have followed a different guide: http://www.mkyong.com/webservices/jax-rs/jersey-hello-world-example/
The strange part is that, in this guide I have used, I should not have any problem with compatibility between versions (1.x against 2.x) because following the guide you use the jersey 1.8.x on pom.xmland in the web.xmlyou refer to a class (com.sun.jersey.spi.container.servlet.ServletContainer) as said before of 1.x version. So as I can infer this should be working.
My guess is because I'm using JDK 1.7 this class does not exist anymore.
After, I tried to resolve with the answers before mine, did not helped, I have made changes on the pom.xmland on the web.xml the error changed to: java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
Which supposedly should be exist!
As result of this error, I found a "new" solution: http://marek.potociar.net/2013/06/13/jax-rs-2-0-and-jersey-2-0-released/
With Maven (archetypes), generate a jersey project, likes this:
mvn archetype:generate -DarchetypeGroupId=org.glassfish.jersey.archetypes -DarchetypeArtifactId=jersey-quickstart-webapp -DarchetypeVersion=2.0
And it worked for me! :)
We get this error because of build path issue. 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 --> Java Build Path --> Add Library --> Server Runtime --> Apache Tomcat v7.0
I encountered the same error today although I was using Jersey 1.x, and had the right jars in my classpath. For those who'd like to follow the vogella tutorial to the letter, and use the 1.x jars, you'd need to add the jersey libraries to WEB-INF/lib folder. This will certainly resolve the problem.
you need to add jersey-bundle-1.17.1.jar to lib of project
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<!-- <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> -->
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<!-- <param-name>jersey.config.server.provider.packages</param-name> -->
<param-value>package.package.test</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
You must replace in your 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.test.myproject</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
for this:
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.test.myproject</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
this is Jersey 2.x uses org.glassfish.jersey packages instead of com.sun.jersey (which is used by Jersey 1.x) and hence the exception. Note that also init-param starting with com.sun.jersey won't be recognized by Jersey 2.x once you migrate to JAX-RS 2.0 and Jersey 2.x
if at any moment you use maven, your pom.xml would be this:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.X</version>
</dependency>
replace 2.X for your desire version, e.g. 2.15
A simple workaround is , check whether you have dependencies or libs in deployment assembly of eclipse.probably if you are using tomcat , the server might not have identified the libs we are using . in that case specify it explicitly in deployment assembly.
Coming back to the original problem - java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
As rightly said above, in JAX 2.x version, the ServletContainer class has been moved to the package - org.glassfish.jersey.servlet.ServletContainer. The related jar is jersey-container-servlet-core.jar which comes bundled within the jaxrs-ri-2.2.1.zip
JAX RS can be worked out without mvn by manually copying all jars contained within zip file jaxrs-ri-2.2.1.zip (i have used this version, would work with any 2.x version) to WEB-INF/lib folder. Copying libs to right folder makes them available at runtime.
This is required if you are using eclipse to build and deploy your project.
In pom.xml file we need to add
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.8</version>
</dependency>
The same error and wasted 2+ hours debugging and trying all options. I was not using the Maven/POM, so I could not leverage that solution given by few.
Finally the following resolved it: Adding the jars directly to the tomcat/lib (NOT WEB-INF\lib) folder and restarting the tomcat.
If anyone is trying to build a hello world application using Jersey, I think one of the easiest ways is to follow Jersey documentation.
https://jersey.github.io/download.html
If you are already using maven, it'd take only a few minutes to see the result.
I used below.
mvn archetype:generate -DarchetypeGroupId=org.glassfish.jersey.archetypes -DarchetypeArtifactId=jersey-quickstart-webapp -DarchetypeVersion=2.26
It basically depends on which version jersey you are using. If you are using Jersey ver.1.X.X you need to add
Jersey 1 uses "com.sun.jersey", and Jersey 2 uses org.glassfish. on servlet class tag.
Also, note that also init-param starting with com.sun.jersey won't be recognized by Jersey 2.
And Add all the jar file into WEB-INF lib folder
In my case, it worked after adding the jersey-bundle jar in my tomcat lib.