Jersey java.lang.ClassNotFoundException: com.sun.j

2020-07-09 09:53发布

I'm trying to make a web-service with Tomcat and Eclipse using jersey library. This is my service class:

package com.gontuseries.university;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;


@Path("/university")
public class UniversityRestWs {

    @GET
    @Produces(MediaType.TEXT_HTML)
    public String getHtmlUniversityInfo(){
        return "<html><body>test</body></html>";
    }

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getTextUniversityInfo(){
        return "test";
    }
}

And this is file 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.gontuseries.university</param-value>
 </init-param>
 </servlet>
 <servlet-mapping>
 <servlet-name>Jersey REST Service</servlet-name>
 <url-pattern>/rest/*</url-pattern>
 </servlet-mapping>

When i test my service i get java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer I downloaded jersey from https://jersey.java.net/download.html

Can someone help me? Thanks

10条回答
倾城 Initia
2楼-- · 2020-07-09 10:19

We need to use provide build path to Eclipse. That must resolve issue.

Make sure it has access to all dependencies in: project -> properties -> development assembly -> add -> java build path entries --> maven dependency

jersey bundle dependency

<dependency>
   <groupId>com.sun.jersey</groupId>
   <artifactId>jersey-servlet</artifactId>
   <version>1.17</version>
<dependency>

If you are using maven, best few simple steps will work your application

查看更多
贼婆χ
3楼-- · 2020-07-09 10:24

I was getting this error in an Eclipse project that had previously worked fine, running on Tomcat 7.0. Eventually I solved it by removing the project from Tomcat and adding it back again.

查看更多
兄弟一词,经得起流年.
4楼-- · 2020-07-09 10:27

For me the problem was that the server had no jars in the lib folder. Make sure it has access to all dependencies in: project -> properties -> development assembly -> add -> java build path entries.

查看更多
孤傲高冷的网名
5楼-- · 2020-07-09 10:32

Man, I had the same problem. I solve id adding the jersey-servlet-1.18.jar to the WEB-INF/lib. Then, this folder saw like this:

.
Remember, add all this jars to yout build-path

查看更多
登录 后发表回答