16

I'd like to create and run a simple RESTful application on my pc. I'm using Java programming language, on Eclipse Helios IDE, with Apache Tomcat 7 server, on a Microsoft Windows Xp operating system.

I've been trying to replicate the same procedure of this tutorial (from 3 to 3.4 steps) on my pc, but it does not work.

Here's what I did:

  • created a Dynamic project on Eclipse, with Apache Tomcat 7 settings

  • put my Jersey and jsr311-api-1.0 jar files into the project \WEB-INF\lib\ directory

  • copied the code parts from the webpage to my project

  • right-clicked on the project, clicked on "Run as -> Run on server"

  • tried to reach the http://localhost:8080/de.vogella.jersey.first/rest/hello

But there's what I see:

Rest screenshot

Why does it not work? What did I do wrong? Why?

Many thanks!

Edit:

Here's what the console says:

25-set-2012 9.35.07 org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Programmi\Java\jdk1.6.0_21\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Programmi/Java/jre6/bin/client;C:/Programmi/Java/jre6/bin;C:/Programmi/Java/jre6/lib/i386;C:\WINDOWS\;C:\WINDOWS\System32;"C:\ApacheMaven\apache-maven-3.0.4\bin";C:\WINDOWS\;C:\WINDOWS\System32;"C:\ApacheMaven\apache-maven-3.0.4\bin";C:\WINDOWS\;C:\WINDOWS\System32;"C:\ApacheMaven\apache-maven-3.0.4\bin";C:\WINDOWS\System32;C:\WINDOWS\;C:\WINDOWS\System32; C:\Programmi\R-Language\R-2.13.1\bin\;C:\Programmi\OpenVPN\bin\; C:\Programmi\Java\jdk1.6.0_21\bin\; "C:\ApacheMaven\apache-maven-3.0.4\bin";;C:\Programmi\eclipseEC;
25-set-2012 9.35.08 org.apache.tomcat.util.digester.SetPropertiesRule begin
AVVERTENZA: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:de.vogella.jersey.first' did not find a matching property.
25-set-2012 9.35.10 org.apache.coyote.AbstractProtocolHandler init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
25-set-2012 9.35.10 org.apache.coyote.AbstractProtocolHandler init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
25-set-2012 9.35.10 org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 3459 ms
25-set-2012 9.35.10 org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
25-set-2012 9.35.10 org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.6
25-set-2012 9.35.11 org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:com:sun:jersey:api:view is already defined
25-set-2012 9.35.12 org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:com:sun:jersey:api:view is already defined
25-set-2012 9.35.12 org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:com:sun:jersey:api:view is already defined
25-set-2012 9.35.12 org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet Jersey REST Service as unavailable
25-set-2012 9.35.12 org.apache.catalina.core.StandardContext loadOnStartup
GRAVE: Servlet /de.vogella.jersey.first threw load() exception
java.lang.ClassNotFoundException: javax.servlet.Filter
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1558)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1488)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:415)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:397)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1048)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:996)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4741)
    at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5062)
    at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5057)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)
25-set-2012 9.35.12 org.apache.coyote.AbstractProtocolHandler start
INFO: Starting ProtocolHandler ["http-bio-8080"]
25-set-2012 9.35.12 org.apache.coyote.AbstractProtocolHandler start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
25-set-2012 9.35.12 org.apache.catalina.startup.Catalina start
INFO: Server startup in 1790 ms

Maybe there's some class missing?

DavideChicco.it
  • 3,318
  • 13
  • 56
  • 84
  • Do your logs say anything informative from the time you start the webapp up? What does its `web.xml` say? – Donal Fellows Sep 25 '12 at 00:02
  • @DonalFellows There is some message in the console, I've edited the question+ – DavideChicco.it Sep 25 '12 at 07:36
  • This may be related http://stackoverflow.com/questions/8856308/java-lang-noclassdeffounderror-javax-servlet-filter – pd40 Sep 27 '12 at 10:48
  • Have you checked http://www.vogella.com/code/de.vogella.jersey.first/WebContent/WEB-INF/web.html about the web.xml file also? You didn't mention that on your question, but the example on vogella gives this file for you. – mico Sep 27 '12 at 19:29
  • 1
    @pd40 It's similar, but unfortunately does not help ... – DavideChicco.it Sep 28 '12 at 14:23
  • @mico My web.xml file is the same of the tutorial. – DavideChicco.it Sep 28 '12 at 14:23
  • Try to use Tomcat 6. Maybe you have to replace the javax.servlet.Filter class. Verify this post for further information: http://stackoverflow.com/questions/8856308/java-lang-noclassdeffounderror-javax-servlet-filter – Bruno Vieira Oct 01 '12 at 16:47
  • To exclude the one and other, do other Eclipse dynamic web projects work fine on the very same Tomcat server? Are you *absolutely positive* that you don't have fiddled with loose servletcontainer-specific JAR files like `servlet.jar`, `j2ee.jar`, etc before? And also that you have *never* placed JAR files in JRE's `/lib` and/or `/lib/ext` folders in order to "fix" `javac` compile and/or `java` runtime errors? In any way, your Java environment is a serious mess if this exception shows up. – BalusC Oct 04 '12 at 02:10
  • As you're apparently using Maven, another possibility would be that it's seriously wrong configured. Do you exactly understand what's already available (provided) by Tomcat 7 and what you've to add on top of it yourself? – BalusC Oct 04 '12 at 02:16

13 Answers13

22

To solve your problem, without using Maven, you need to add JAX-RS facet to your project. What's happening here is that Eclipse is not deploying the right libs to the server. This configuration can be accessed through your project's Properties | Project's Facets.

To be honest I don't know which specific jars to add to make Jersey work, because I use Maven to manage dependencies. So instead of the zip file, I've downloaded the Jersey bundle, and configured the JAX-RS facet with it. The effect of this can be seen on the project's Properties | Deployment Assembly, where Jersey's libs are already configured to be deployed. By the way is there you can manually setup the deployed libs to the server.

But then I remembered why I use Maven to manage dependencies. The bundle does not contain everything it needs to run Jersey. It's missing the asm jar (present in the zip file). So I had to add it to projects build path, and reconfigure de deployment assembly manually....

So, here is a tutorial with Maven, Jersey and Eclipse. Try it and draw your conclusions :)

Gilson Costa
  • 431
  • 2
  • 2
3

I went through the same tutorial to develop my first RESTFUL service. In the beginning i also got 404 errors few times so i kept changing few things, finally it worked for me. This is what i did-

1) removed the rest from the URL-Pattern tab; i think that is there by mistake. 2) used the context root in the URL rather than the package name.

my url is, "http://mylocalhost:8080/contextroot/classname that is defined in the @path annotation.

hope it helps.

Lokesh Mehra
  • 545
  • 6
  • 16
Aruna
  • 31
  • 1
2

The servlet.jar shouldn't be at the resulting war file (or in WEB-INF/lib). It should be provided by the application server itself. (see Tomcat's FAQ) If you are using the Maven for building the project, you may set the scope of the servlet api as provided (link).

Another similar question here on StackOverflow.

Community
  • 1
  • 1
Jiri Kremser
  • 12,471
  • 7
  • 45
  • 72
  • Thanks, but where should I put the ... code part? Which file in? – DavideChicco.it Oct 01 '12 at 14:30
  • When using Maven, the dependencies are described in the pom.xml file. If you are not using Maven, you can compile the project with linked servlet.jar library, but then make sure it is the same version like the one used by the application server, and make sure it is not in the resulting war file (or WEB-INF/lib) when deployed. – Jiri Kremser Oct 01 '12 at 21:46
2

I repeated your example and it works out of the box on my machine. I downloaded jersey-archive-1.14.zip and put the whole content of the included lib directory into WEB-INF/lib, not just Jersey and jsr311-api-1.0 jar as you did.

The jars are:

  • asm-3.1.jar
  • jackson-core-asl-1.9.2.jar
  • jackson-jaxrs-1.9.2.jar
  • jackson-mapper-asl-1.9.2.jar
  • jackson-xc-1.9.2.jar
  • jersey-client-1.14.jar
  • jersey-core-1.14.jar
  • jersey-json-1.14.jar
  • jersey-server-1.14.jar
  • jersey-servlet-1.14.jar
  • jettison-1.1.jar
  • jsr311-api-1.1.1.jar

Some of them might by unnecessary, you could try to reduce them. But start with all of them.

The error message is strange, though. It says that javax.servlet.Filter is missing, which should live in <your tomcat dir>/lib/servlet-api.jar. This file is always on the classpath when you run tomcat.

ChrLipp
  • 15,526
  • 10
  • 75
  • 107
1

You need to remove servlet.jar or any other jar containing javax.servlet.Filter from your WEB-INF/lib directory.

While you will need the class javax.servlet.Filter on your compile classpath in order for the sample code to compile, you need to ensure that you do not include this class in your webapp. From the Tomcat class loader HOWTO:

When Tomcat is started, it creates a set of class loaders that are organized into the following parent-child relationships, where the parent class loader is above the child class loader:

  Bootstrap
      |
   System
      |
   Common
   /     \   

Webapp1 Webapp2 ...

Basically, the (probably) servlet.jar in the webapp is probably not being found properly d/t the security configured in the Tomcat container.

You can see if javax.servlet.Filter is being loaded from a weird location by turning on verbose class loading verbose:class. Search for the class, if it's not coming from the Tomcat libs, that is likely your problem.

stevedbrown
  • 8,862
  • 8
  • 43
  • 58
0

May be you are missing servlet-api.jar. Add latest jar in your /WEB-INF/lib or classpath and see of it works or not.

Jeevan Patil
  • 6,029
  • 3
  • 33
  • 50
  • AS should provide the servlet api. – Jiri Kremser Oct 01 '12 at 21:47
  • No. It is wrong. Your application should never contains servlet-api.jar in WEB-INF\lib For example, Tomcat will ignore it, if found. http://www.simplysimple.info/tomcat-error-jar-not-loaded-servlet-spec-23-section-972-offending-class-javaxservletservletclass/ – user1516873 Oct 03 '12 at 08:12
0

It seems that it does not find the servlet jersey jars in the classpath. Make sure to include them. I assume that you package into a war and deploy before you test this right. So just check if the war includes all the jars that were available to your application at compile time.

Nikhil Das Nomula
  • 1,863
  • 5
  • 31
  • 50
0

Like previous answers mentioned. You are missing some jars. You can either add those jar files in Project>WebContent>WEB-INF>lib> folder or in your webserver's lib folder (for example, \apache-tomcat\lib )

Try pasting list or screenshot of your both lib folders, if you'd like us to tell you what exact jar you are missing.

Hope this helps

Ellipsis
  • 117
  • 7
  • 1
    In \WebContent\WEB-INF>lib\ I have: asm-3.1.jar, jersey-bundle-1.14.jar. In \apache-tomcat-7.0.6\lib\ I have: annotations-api.jar, catalina-ant.jar, catalina-ha.jar, catalina-tribes.jar, catalina.jar, ecj-3.6.jar, el-api.jar, jasper-el.jar, jasper.jar, jsp-api.jar, servlet-api.jar, tomcat-api.jar, tomcat-coyote.jar, tomcat-dbcp.jar, tomcat-i18n-es.jar, tomcat-i18n-fr.jar, tomcat-i18n-ja.jar, tomcat-util.jar. Thanks! – DavideChicco.it Oct 01 '12 at 14:11
  • @DavideChicco.it so, where jsr311-api-1.0.jar? You should have it in \WebContent\WEB-INF>lib\ – user1516873 Oct 03 '12 at 07:56
0

As far as I can tell, you are running Tomcat from within Eclipse. Is is possible you are not using the installation directory in your computer (i.e. the one where you have copied the JARs to) but are in fact using the temporary copy that Eclipse creates locally (sorry, I'm not on my dev computer atm so can't check the details)?

From memory you have 2 options:

  1. You need to change the (working) directory that Eclipse is using for the Tomcat to be the one where you actually have the Tomcat files.

  2. You need to add the JARs to the Tomcat in Eclipse with the GUI interface within Eclipse.

Alternatively, I could be completely wrong here ;-)

EDIT: Including a screenshot of the bit I'm talking about.

For my options above:

  1. See the "Sever Locations" in the middle needs to be in the one that "Takes control of the tomcat installation".
  2. You add them in the classpath tab when you click the "Open launch configuration" link in the "General Information" box in the image below

enter image description here

Doddie
  • 1,393
  • 13
  • 21
0

You should add all jars, that you will find in jersey 1.6 archive, download this archive and add all jars to WebContent/WEB-INF/lib folder of your project.

Hulk1991
  • 3,079
  • 13
  • 31
  • 46
Ajit
  • 957
  • 1
  • 8
  • 24
0

I had this error for more than one day! It turns out that the problem comes from the usage of different versions of Jersey. If you replace your web.xml content by the content I present below, it will be fine!

    <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>de.vogella.jersey.first</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>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>

Hopefully, my answer will save lots of time for someone else who also decides to follow the same tutorial!

ccoutinho
  • 3,308
  • 5
  • 39
  • 47
0

Delete the servlet.jar from WEB-INF/lib folder. Servlet.jar shouldn't be there this fixed the problem for me.

Amrit
  • 11
  • 1
0

I had the HTTP Status 404 issue too, following the same tutorial.

First I tried to set JAX-RS Project Facet in Eclipse with either Jersey libraries 1.18 and 2.15 but it didn't work.

Finally I solved the problem by replacing the package name (de.vogella.jersey.first) in the URL with the context root (i.e. the eclipse project name).

This tutorial by mkyong.com gave me the hint to find the solution. Anyway I don't know if the package name in the URL is definitely a mistake or if there are some configuration details that could make it work as well.