简体   繁体   中英

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

My Node.JS API (localhost) call a Jersey API on the Tomcat server (localhost) worked really well previously, but I don't know why it accidentally failed today. I have checked that all files have not modified since last week. Once I tried to GET http://localhost:8080/graybox/rest/analytic

It will fail and show these message in the eclipse console.

SEVERE: Allocate exception for servlet [Jersey Web Application]
java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1291)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:544)
    ...

I have surveyed a lot but I still can't find a way to solve my problem. The following is what I have done previously try to resolve this issue, which also attaches dev environment, web.xml and pom.xml. Thanks.

  1. Install maven (brew), m2eclipse (Eclipse install new software), m2e-wtp (Eclipse install new software) Eclipse install new software: Help -> Install New Software -> copy paste the url into "Work with" -> Choose all and Next and Finish m2eclipse m2e-wtp
  2. Eclipse: Import -> Existing Maven project -> choose "graybox" project folder
  3. Rebuild Maven global index in Eclipse: Detail
  4. Add dependencies in Maven to make sure all jersey dependencies in the same version eg all 1.19 version jersey-server, jersey-bundle, jersey-core, jersey-json Reasons
  5. Clean Maven dependencies
    1. Project -> Clean -> Clean
    2. Right click project -> Maven -> Update Project
    3. Right click project -> Run as -> 4 (or 5) Maven build -> Goals: clean install -> Apply -> Run Detail
  6. If there are more than one Tomcat servers running, remove unrelated Tomcat servers, make sure choose the right one in runtime: Window -> Preferences -> Server -> Runtime Environments -> Remove all unrelated servers
  7. Add server Tomcat Detail Before running the server, double-click the server -> Server Locations: Use Tomcat installation … Detail
  8. Right click project -> Properties -> Project facets -> Choose: Dynamic Web Module, Java, JavaScript, JAX-RS (REST Web Services) -> Runtimes -> Choose the right server -> Apply -> Apply and Choose
  9. Start server -> Run project -> browser http://localhost:8080/ -> show welcome page of Tomcat

Environment

  • Mac os x 10.13.2, x86_64
  • Eclipse Oxygen.2 Release (4.7.2)
  • Back-end
    • Java jdk1.8.0_151-b12
    • Tomcat 8.5
    • Maven 3.5.2 Default locale: en_AU, platform encoding: UTF-8
    • Jersey 1.19
    • Node 8.9.4
    • NPM 5.6.0
    • Bower 1.8.2
    • Nodemon 1.14.11

This is 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>Graybox</display-name>
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

This is 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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>graybox</groupId>
  <artifactId>graybox</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <finalName>graybox</finalName>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.3.4</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.0</version>
    </dependency>
    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-server</artifactId>
      <version>1.19</version>
    </dependency>
    <dependency>
      <groupId>net.sourceforge.jexcelapi</groupId>
      <artifactId>jxl</artifactId>
      <version>2.6.10</version>
    </dependency>
    <dependency>
      <groupId>asm</groupId>
      <artifactId>asm</artifactId>
      <version>3.3.1</version>
    </dependency>
    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-bundle</artifactId>
      <version>1.19</version>
    </dependency>
    <dependency>
      <groupId>org.json</groupId>
      <artifactId>json</artifactId>
      <version>20170516</version>
    </dependency>
    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-core</artifactId>
      <version>1.19</version>
    </dependency>
    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-json</artifactId>
      <version>1.19</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-servlet</artifactId>
        <version>1.19</version>
    </dependency>
  </dependencies>
</project>

Due to accidentally shut down my Eclipse, it seems that the Java Build Path doesn't save Maven. It is the same as one of the answers answered by Adi

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

Right click your eclipse project Properties -> Deployment Assembly -> Add -> Java Build Path Entries -> Gradle Dependencies -> Finish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM