Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

eclipse - java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer

You may feel this is a duplicated question, but none of the questions with the same title solve my problems. I am using Jersey 2.0 creating a RESTful web service in Eclipse, I use Tomcat 7.0 as my server, I have the following 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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <servlet>
    <servlet-name>JAX-RS Servlet</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.shop.domain</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>JAX-RS Servlet</servlet-name>
    <url-pattern>/jaxrs/*</url-pattern>
  </servlet-mapping>
</web-app>

I have a simple class called Hello:

@Path("customers")
public class Hello {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getCustomer() {
        return "hello";
    }
}

I have a Jersey library called jersey:

enter image description here

Every time I ran this project, I got error of

java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I guess this has already been answered, but just wanted to add an extra tip.

For web projects in Eclipse, it is almost always advisable to manage dependencies with Maven. Use a Maven plugin like m2e. Once you add a dependency to your Maven project, it should automatically deploy those libraries to WEB-INF/lib. If it does not (for whatever reasons), you can explicit do so:

  1. Right-click on your project in Project Explorer
  2. Select Properties
  3. Select Deployment Assembly
  4. If Maven Dependencies is not one of the entries, the libraries were not added automatically, so we'll add them now
  5. Select Add
  6. Select Java Build Path Entries
  7. Select Maven Dependencies
  8. Click Finish

This should add the libraries to WEB-INF/lib, although you'd still not see them in the Project Explorer view. But your ClassNotFoundException should go away now.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...