permaculture playing cards*
The moose likes Tomcat and the fly likes NoClassDefFoundError: HttpServlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "NoClassDefFoundError: HttpServlet" Watch "NoClassDefFoundError: HttpServlet" New topic
Author

NoClassDefFoundError: HttpServlet

Vince Aggrippino
Greenhorn

Joined: May 19, 2006
Posts: 14
I'm using Apache Tomcat 6.0.14 with Java 1.6.0_02.

I've been having some trouble loading servlets, so I tried a basic HelloWorld type program just to make sure everything was okay and it can't find the servlet-api.jar.

I'm getting NoClassDefFoundError: javax/servlet/http/HttpServlet

Isn't it supposed to automatically load all of the jar files in $CATALINA_HOME/lib?

Here's my code:

Here's my web.xml:

Thank you,

--
-- Vince


-- Vince
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5


All classes used in servlets should be in a package - thats the first thing to correct.

If Tomcat runs and you can execute the example servlet and JSPs provided then your installation has all it needs.

Bill
Vince Aggrippino
Greenhorn

Joined: May 19, 2006
Posts: 14
Originally posted by William Brogden:


All classes used in servlets should be in a package - thats the first thing to correct.

If Tomcat runs and you can execute the example servlet and JSPs provided then your installation has all it needs.

Bill

I realize that all classes used in servlets should be in a package as a matter of convention and good programming practice. As you might have guessed, all of my other servlets have packages. However it's not an error not to use a package.

I added a package to my test servlet, recompiled, and restarted Tomcat, but there was no change.

Tomcat runs and I can run all of the example servlets and JSPs provided.

I did notice that I don't have the problem if I'm only attempting to load a single servlet with Tomcat. If I only add one web application to the webapps folder, it runs fine.
[ September 03, 2007: Message edited by: Vince Aggrippino ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Vince Aggrippino:

I realize that all classes used in servlets should be in a package as a matter of convention and good programming practice. As you might have guessed, all of my other servlets have packages. However it's not an error not to use a package.


That's not entirely correct.
There are times when the container will not be able to find classes that are not packaged. This is not one of them but, if your having trouble getting something to work, you should start buy ruling this problem out.
You can read more about this here:
http://faq.javaranch.com/java/PackageYourBeans


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You don't have j2ee.jar in your JDK directory anywhere do you?
I've heard of people having trouble with Tomcat when they do.
Vince Aggrippino
Greenhorn

Joined: May 19, 2006
Posts: 14
Originally posted by Ben Souther:
You don't have j2ee.jar in your JDK directory anywhere do you?
I've heard of people having trouble with Tomcat when they do.


No, I don't. I was hoping that was the solution

I have made a little progress...
I deleted my tomcat installation directory and untarred it again and now basic HelloWorld type servlets will run. I'm not getting a NoClassDefFound error for HttpServlet any more, but I'm still getting the problem and I think it's related.

I'll post a new thread with a new subject.

Thank you,
Vince
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
However it's not an error not to use a package.


Wow what an amazing statement! It is true that Tomcat will not raise an error when it sees that in a web.xml statement - probably a consequence of the dreaded invoker servlet use in early versions.

Hard to figure out errors due to the package thing are the base of a large fraction of beginner problems fielded here at the ranch. Just grit your teeth, bite the bullet etc etc and put all classes used in servlets in a package.

Bill
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

but I'm still getting the problem and I think it's related.


What problem are you seeing now?
Vince Aggrippino
Greenhorn

Joined: May 19, 2006
Posts: 14
Originally posted by Ben Souther:


What problem are you seeing now?


Thank you, Ben. I think I figured it out.

The problem seemed intermittent and now I know why. It depended on what directory I was in when I started Tomcat.

I wanted to save myself some typing, so I made symbolic links to $CATALINA_HOME/bin/startup.sh and $CATALINA_HOME/shutdown.sh and put them in ~/bin, which I've added to my path.

I know there is some code in those shell scripts to detect if the command that called it is a symbolic link and take appropriate actions based on that, but I guess it doesn't work perfectly.

I've changed my symlinks to actual scripts, with two lines each. Change to the Tomcat directory, then run startup.sh/shutdown.sh. Now everything works fine every time

Thank you all for your time and consideration.

-- Vince
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Glad it's working.
Thanks for posting back with your solution.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NoClassDefFoundError: HttpServlet
 
Similar Threads
NoClassDefFoundError while running a servlet
NoClassDefFoundError
NoClassDefFoundError Problem with running a jasper report
deploying prb ..
Problem deploying-accesing a single servlet webapp in Tomcat 5