This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes NoClassDefFoundError while running a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NoClassDefFoundError while running a servlet" Watch "NoClassDefFoundError while running a servlet" New topic
Author

NoClassDefFoundError while running a servlet

srinivas pola
Greenhorn

Joined: Jul 18, 2008
Posts: 24
When i'm trying to run a servlet through command prompt i'm getting this error.

C:\tomcat6\webapps\HeadFirst\WEB-INF\classes\web>java BeerSelectAction
Exception in thread "main" java.lang.NoClassDefFoundError: BeerSelectAction
Caused by: java.lang.ClassNotFoundException: BeerSelectAction
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)



Here is my xml file:
<web-app 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/web-app_2_4.xsd"
version="2.4">

<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>classes.web.BeerSelectAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>


The servlet i'm trying to run:

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

public class BeerSelectAction extends HttpServlet {

public void doPost(HttpServletRequest request,
HttpServletResponse response) throws
IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();
out.println("Beer Selection Advice<br>");

String c = request.getParameter("color");
out.println("<br>Got beer color" +c);
}
}

Please help me out.
Thanks in advance
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817


srinivas pola:
--------------------------------
When i'm trying to run a servlet through command prompt i'm getting this error


The servlet class file(after compiling it), should be in appropiate location where the server can identify it (eg: WebAppName/WEB-INF/classes/"packagename"). Then restart the server (if you are deploying as a web application(WAR file etc..) no need to restart,just deploy). Then access the servlet through your browser.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

You cannot run a servlet from the command line like that. A servlet is not a stand-alone Java program. You must deploy it in a servlet container (Tomcat for example) and access it using a web browser.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
srinivas pola
Greenhorn

Joined: Jul 18, 2008
Posts: 24
I'm using tomcat6 for deployment. I'm not using any IDE so i have compiled the java file through command promt.

When i try to load the servlet through the browser its not getting loaded i dont know where the problem is.

Please help solve this...
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

where have you put it? Post the exact path in the servlet in your Tomcat server.

You may create a empty folder(MyAPP) and create a WEB-INF folder in their and put your web.xml in to it, and create a sub folder called classes in WEB-INF and put the servlet class in there with the correct package heirarchy. Then copy hole MyApp folder in to Tomcat webapps folder. Then you may access your servlet from the browser by http://hostname ortnumber/MyApp/urlmappingtoyourservlet. Hope this helps.
srinivas pola
Greenhorn

Joined: Jul 18, 2008
Posts: 24
C:\tomcat6\webapps\HeadFirst\WEB-INF\classes\web\BeerSelectAction.java
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

C:\tomcat6\webapps\HeadFirst\WEB-INF\classes\web\BeerSelectAction.java


this is java source file, You should put the class file there and by the way change your web.xml's

in to


and restart the server and try

http://hostname:portnumber/HeadFirst/anything.do

[edit]Disable smilies. CR[/edit]
[ September 25, 2008: Message edited by: Campbell Ritchie ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41150
    
  45
As was pointed out to you in the other tread, your code is lacking a "package web;" statement.


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NoClassDefFoundError while running a servlet
 
Similar Threads
Servlet code problem....from HFSJ
javax.servlet.ServletException: Error allocating a servlet instance with Tomcat 6.0.20 on windows 7
help me solve 'package javax.servlet does not exist' error
Problem getting the Beer Selection Page screen shot
servlet programming problem in Eclipse