• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NoClassDefFoundError while running a servlet

 
srinivas pola
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3918
10
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
srinivas pola
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3918
10
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C:\tomcat6\webapps\HeadFirst\WEB-INF\classes\web\BeerSelectAction.java
 
Vijitha Kumara
Bartender
Pie
Posts: 3918
10
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As was pointed out to you in the other tread, your code is lacking a "package web;" statement.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic