[java] HTTP Status 500 - Error instantiating servlet class pkg.coreServlet

I am creating simple servlet and deploying it in tomcat server but I am getting the following error:

HTTP Status 500 - Error instantiating servlet class pkg.coreServlet

File Structure on the tomcat server:

webapps     
| 
- aarya
  |
  - WEB-INF
    |
     -web.xml
     -src(folder)
       |
       -pkg
         |
         -coreServlet.class

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


<servlet>
    <servlet-name>aaryaservlet</servlet-name>
    <servlet-class>pkg.coreServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>aaryaservlet</servlet-name>
        <url-pattern>/coreServlet</url-pattern>
    </servlet-mapping>
</web-app>

coreServlet.java:

package pkg;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*; 

public class coreServlet extends HttpServlet
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest req,HttpServletResponse res)
 throws ServletException,IOException


   {
        PrintWriter out = res.getWriter();
        res.setContentType("text/html");
        out.println("this is First servlet Example ");
    }
}

url I am giving is http://localhost:8080/aarya/coreServlet I try by restarting tomcat but I am getting same error. Where I am doing wrong?

This question is related to java tomcat servlets classnotfoundexception

The answer is


Do not put the src folder in the WEB-INF directory!!


Change the

private static final long serialVersionUID = 1L;

to any other value like

private static final long serialVersionUID = 102831973239L;

also you can generate it automatically in eclipse.

It is because each servlet in a app has a unique id.and tomcat causes problem with two servlets having same id...


Make sure the following:

  1. Proper "war" file structure i.e. WEB-INF & META-INF
  2. "web.xml" file is setup correct.
  3. Last and important: private static final long serialVersionUID = 1L; should be there in your class (<servlet-class>MyClass</servlet-class>).

Have you closed the < web-app > tag in your web.xml? From what you have posted, the closing tag seems to be missing.


Try This:)

before:-

<servlet>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>TestServlet</servlet-class>  
</servlet>

After:-

 <servlet>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>operation.TestServlet</servlet-class>
 </servlet>

The servlet class should be in the WEB-INF/classes not WEB-INF/src.


In my case missing private static final long serialVersionUID = 1L; line caused the same error. I added the line and it worked!


I had an issue with Servlet instantiation. I cleaned the project and it worked for me. In eclipse menu, Go to Project->Clean. It should work.


The above error can occur for multiple cases during servlet startup / request. Hope you check the full stack trace of the server log, If you have tomcat, you can also see the exact causes in html preview of the 500 Internal Server Error page.

Weird thing is, if you try to hit the request url a second time, you would get 404 Not Found page.

You can also debug this issue, by placing breakpoints on all the classes constructor initialization block, whose objects are created during servlet startup/request.

In my case, I didn't had javaassist jar loaded for the Weld CDI injection to work. And it shown NoClassDefFound Error.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to tomcat

Jersey stopped working with InjectionManagerFactory not found The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. on deploying to tomcat Spring boot: Unable to start embedded Tomcat servlet container Tomcat 404 error: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists Spring Boot application in eclipse, the Tomcat connector configured to listen on port XXXX failed to start Kill tomcat service running on any port, Windows Tomcat 8 is not able to handle get request with '|' in query parameters? 8080 port already taken issue when trying to redeploy project from Spring Tool Suite IDE 403 Access Denied on Tomcat 8 Manager App without prompting for user/password Difference between Xms and Xmx and XX:MaxPermSize

Examples related to servlets

Google Recaptcha v3 example demo Difference between request.getSession() and request.getSession(true) init-param and context-param java.lang.NoClassDefFoundError: org/json/JSONObject how to fix Cannot call sendRedirect() after the response has been committed? getting error HTTP Status 405 - HTTP method GET is not supported by this URL but not used `get` ever? Create a simple Login page using eclipse and mysql Spring get current ApplicationContext insert data into database using servlet and jsp in eclipse What is WEB-INF used for in a Java EE web application?

Examples related to classnotfoundexception

A child container failed during start java.util.concurrent.ExecutionException org.glassfish.jersey.servlet.ServletContainer ClassNotFoundException How do I resolve ClassNotFoundException? HTTP Status 500 - Error instantiating servlet class pkg.coreServlet oracle.jdbc.driver.OracleDriver ClassNotFoundException java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener Why, Fatal error: Class 'PHPUnit_Framework_TestCase' not found in ...? JUNIT Test class in Eclipse - java.lang.ClassNotFoundException java.lang.ClassNotFoundException on working app