DJ-
DJ-

Reputation: 119

SEVERE: Servlet /JAXRS-HelloWorld threw load() exception java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer

I read through plenty of posts related to this issue but couldn't get any help .

ERROR Appreciate if someone can help me out.

enter image description here

This is how my POM and web looks like . I have tried copying the required war file to C:\appache tomcat 7 but no luck. Even deploying it within the eclipse also gives me the same error.

WEb enter image description here

POM.xML:-

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.tutorialacademy.rest</groupId>
  <artifactId>helloworld</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>helloworld Maven Webapp</name>
  <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <repositories>  
 <repository>  
  <id>maven2-repository.java.net</id>  
  <name>Java.net Repository for Maven</name>  
  <url>http://download.java.net/maven/2/</url>  
  <layout>default</layout>  
 </repository>  
</repositories>  

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.19</version>
        </dependency>
     <dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-apache-client</artifactId>
    <version>1.19</version>
</dependency>
    </dependencies>



    <build>
        <sourceDirectory>src/main/java</sourceDirectory>

        <plugins>

            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

            <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-compiler-plugin</artifactId>
               <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
               </configuration>
          </plugin>


        </plugins>

    </build>
</project>

Upvotes: 0

Views: 12894

Answers (2)

santoshM
santoshM

Reputation: 267

If you are trying to make a Web application to provide web service using Jersey library in Tomcat and Eclipse and still getting problem after adding jersey-server.jar in the classpath. You can follow these steps to add dependency on deployment assembly.

Project -> properties -> development assembly -> add -> java build path entries -> maven dependency

This should work.

Upvotes: 0

cassiomolin
cassiomolin

Reputation: 130857

By the exception, it looks like the com.sun.jersey.spi.container.servlet.ServletContainer class is not available in your classpath.

Note that Jersey 1.x and Jersey 2.x use different package names:

  • Jersey 1.x: com.sun.jersey
  • Jersey 2.x: org.glassfish.jersey

So, I understand you are using Jersey 1.x.

Jersey 1.x depencency

To use Jersey 1.x, you need the following dependecy in your pom.xml:

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.19</version>
</dependency>

To read more about Jersey 1.x dependencies, have a look at the documentation.

You can check the latest version of the jersey-server artifactId in the Maven Repository.

Jersey 2.x depencency

If you want to use Jersey 2.x, you have to add the following dependency to your pom.xml:

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <!-- if your container implements Servlet API older than 3.0, 
         use "jersey-container-servlet-core"  -->
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.22.1</version>
</dependency>

You can check the latest version of the jersey-container-servlet artifactId in the Maven Repository.

Read more about Jersey 2.x dependencies in the documentation.


UPDATE

If you can, I do recommend using Jersey 2.x rather than Jersey 1.x.

To do it, use the following pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.tutorialacademy.rest</groupId>
    <artifactId>helloworld</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>Hello World</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <repositories>  
        <repository>  
            <id>maven2-repository.java.net</id>  
            <name>Java.net Repository for Maven</name>  
            <url>http://download.java.net/maven/2/</url>  
            <layout>default</layout>  
        </repository>  
    </repositories>  

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <!-- if your container implements Servlet API older than 3.0, 
                 use "jersey-container-servlet-core"  -->
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.22.1</version>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

And the following web.xml:

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

</web-app>

Upvotes: 5

Related Questions