2
votes

I`m using java and eclipse to make a simple rest service. I use jersey 1.19 and jackson 1.7.9. But when i try to run project with tomcat i have this exception.

SEVERE: Servlet [jersey-servlet] in web application [/EmployeeService] threw load() exception java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571) at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:506) at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:488) at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:115) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1148) at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087) at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5262) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5550) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565) at java.util.concurrent.FutureTask.run(FutureTask.java:262) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745)

Here i will copy the classes and web.xml which i have.

@XmlAccessorType(XmlAccessType.NONE)

@XmlRootElement(name = "employee") public class Employee {

public String empId;
public String name;
public String email;

@javax.xml.bind.annotation.XmlElement(required = true)
public String getEmpId() {
    return empId;
}

public void setEmpId(String empId) {
    this.empId = empId;
}

@javax.xml.bind.annotation.XmlElement(required = true)
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@javax.xml.bind.annotation.XmlElement(required = true)
public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

}

@Path("/emp") public class EmployeeService {

@GET
@Path("/get/{empId}")
public Employee getEmployee(@PathParam("empId") String empId) {
    Employee employee = new Employee();
    employee.setEmpId(empId);
    employee.setName("Nikola");
    employee.setEmpId("[email protected]");

    return employee;
}

@POST
@Path("/create")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_XML)
public Employee createEmployee(Employee employee) {
    // create logic goes here
    return employee;
}

@PUT
@Path("/update")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Employee updateEmployee(Employee employee) {
    // create logic goes here
    employee.setName(employee.getName() + "updated");
    return employee;
}

@DELETE
@Path("/delete/{empId}")
public javax.ws.rs.core.Response deleteEmployee(@PathParam("empId") int empId) throws URISyntaxException {
    return javax.ws.rs.core.Response.status(200).entity("Employee with " + empId + "is deleted successfully").build();
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> EmployeeProvider index.html index.htm index.jsp default.html default.htm default.jsp

<servlet>   
    <servlet-name>jersey-servlet</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.rest.employee</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jersey-servlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

4
Do you have the jersey-servlet-1.19.jar?Paul Samsotha
Yep, i`ve add that jar :)Metala

4 Answers

1
votes

I had same issue. I put all jar into referenced libraries. but you have to Just copy-paste all required jar files into WEB-INF/lib folder. Problem solved.

1
votes

We get this error because of build path issue.You should add "Server Runtime" libraries in Build Path.

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

Please follow below steps to resolve class not found exception.

Right click on project --> Build Path --> Build Path --> Add Library --> Server Runtime --> Apache Tomcat v7.0

and yeah, remove load on startup tag.

It should work fine.

0
votes

you should remove this on your web.xml

 <load-on-startup>1</load-on-startup>
0
votes

I understand that you have added the jersey-servlet-1.19.jar but is it in your build path?