0
import java.io.*;
 
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
 
public class getname extends HttpServlet { 
	public void init(ServletConfig config) throws ServletException {	
		super.init(config);
	} 
	public void destroy() {
 
	} 
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
		String name = null;
		PrintWriter out = response.getWriter();
		if(request.getParameter("txtname") != null) {
			name = request.getParameter("txtname");
		}
                else {
                       name = "";
                }
  	        out.println("You have successfully made Ajax Call:" + name);
	}
}

I compiled the code using the below line: -
C:\>javac -classpath "C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib
\servlet-api.jar" "C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapp
s\tut\WEB-INF\classes\com\mmfsl\web\gettime.java"

This servlet compiled without any errors and generated class file also.

This is my 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">

    <display-name> My Tutorials </display-name>

<servlet>
	<servlet-name>gettime</servlet-name>
	<servlet-class>com.mmfsl.web.gettime</servlet-class>
</servlet>

<servlet-mapping>
	<servlet-name>gettime</servlet-name>
	<url-pattern>/gettime.do</url-pattern>
</servlet-mapping>

<servlet>
	<servlet-name>getname</servlet-name>
	<servlet-class>com.mmfsl.web.getname</servlet-class>
</servlet>

<servlet-mapping>
	<servlet-name>getname</servlet-name>
	<url-pattern>/getname.do</url-pattern>
</servlet-mapping>

    <session-config>
        <session-timeout>20</session-timeout>
    </session-config>

    <welcome-file-list>
        <welcome-file>Login.jsp</welcome-file>
    </welcome-file-list>

    <error-page>
        <error-code>500</error-code>
    </error-page>
</web-app>

HTTP Status 500 -
type Exception report
message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Error instantiating servlet class com.mmfsl.web.getname
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:401)
org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:281)
org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1568)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)

root cause

java.lang.NoClassDefFoundError: com/mmfsl/web/getname (wrong name: getname)
java.lang.ClassLoader.defineClass1(Native Method)
java.lang.ClassLoader.defineClassCond(Unknown Source)
java.lang.ClassLoader.defineClass(Unknown Source)
java.security.SecureClassLoader.defineClass(Unknown Source)
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2778)
org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1139)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1633)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1511)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:401)
org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:281)
org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1568)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.2 logs.
Apache Tomcat/7.0.2

<big><u><b>Can any one help me what it is.</b></u></big>

2
Contributors
7
Replies
21
Views
6 Years
Discussion Span
Last Post by rakesh4java
0

Do you have a package declaration at the top of your servlet? Does the "classes" directory contain the file "getname.class" placed in the folder hierarchy com/mmfsl/web ?

0

Yes you are right. I have done the same way
Inside this folder are the two classes
"C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\tut\WEB-INF\classes\com\mmfsl\web"

<b>1) getname.class </b>

package com.mmfsl.web;
import java.io.*;
import java.text.*; 
import java.util.*; 
import javax.servlet.*; 
import javax.servlet.http.*;
 
public class getname extends HttpServlet {
 
	public void init(ServletConfig config) throws ServletException {	
		super.init(config);
	}
 
	public void destroy() {
 
	}
 
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
		String name = null;
		PrintWriter out = response.getWriter();
		if(request.getParameter("txtname") != null) {
			name = request.getParameter("txtname");
		}
                else {
                       name = "";
                }
  	        out.println("You have successfully made Ajax Call:" + name);
	}
}

<b>2) gettime.class .</b>

package com.mmfsl.web;
import java.io.*; 
import java.text.*; 
import java.util.*; 
import javax.servlet.*; 
import javax.servlet.http.*;
 
public class gettime extends HttpServlet {
 
	public void init(ServletConfig config) throws ServletException {	
		super.init(config);
	}
 
	public void destroy() {
 
	}
 
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
		PrintWriter out = response.getWriter();
		Date df = new Date();
		out.println(df.getTime());
	}
 
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
		doPost(request,response);
	}
}
0

There is something definitely wrong with your setup because I'm able to run the same thing locally without any problems.

Is your Tomcat installation clean or have you made any modifications to it? Are there any other apps successfully running on the same Tomcat installation? What is the entire directory structure of the "tut" directory in the "webapps" folder? How do you start Tomcat?

0

You got it...!
I have another development setup of Tomcat which is of version (Apache Tomcat/6.0.20)
and it worked well. Below is our stable development server

DEV

Server Information
Tomcat Version 	JVM Version 	JVM Vendor 	OS Name 	OS Version 	OS Architecture
Apache Tomcat/6.0.20 	1.7.0-ea-b58 	Sun Microsystems Inc. 	Windows 2003 	5.2 	x86

So far, I was trying in (Apache Tomcat/7.0.2) with my local setup of version
LOCAL DEV

Server Information
Tomcat Version 	JVM Version 	JVM Vendor 	OS Name 	OS Version 	OS Architecture
Apache Tomcat/7.0.2 	1.6.0_22-b04 	Sun Microsystems Inc. 	Windows XP 	5.1 	x86

I think the JVM Version in both the servers are different.
Is that the reason behind this issue......?

In my LOCAL DEV setup few other applications are running perfectly...
I did not modified/tweaked any of the settings in my LOCAL DEV
Directory Structure
C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\tut

Tomcat will run as a service also when windows starts up

Attachments tut_setup.JPG 24.57 KB
0

That's strange. Do you have multiple JVM's installed on your local machine? Which Java version are you using to compile your project i.e. what's your JAVA_HOME and PATH set to?

Even if it were related to different JVM versions, I don't think the error would be NoClassDefFound. Are you sure you have the entire stack trace there? Can you empty the log folder on your local setup, restart the server and paste the *entire* stack trace here?

Also, space in directory path causes real strange issues so can you relocate your local Tomcat directory to something like "c:/tomcat-7" and then try again?

0

JAVA_HOME is C:\Program Files\Java\jdk1.6.0_22\bin
CLASSPATH is C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\servlet-api.jar

I stopped and started the Tomcat Service completely.

I'd re Written WEB.XML file over again and again reloaded the application.
Now it is working.

It's very strange for me too...
Anyways thanks a lot for your suggestions.
I'll ensure the tomcat installation directory does not contain spaces also.
So I'll have to reinstall. I'll do that. Thanks

0

I remember reading somewhere ONLINE that restarting the services will free up the resources and memory leaks. If thats true it worked for me...
I'll close this thread.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.