HttpServlet not found?

Hi,

I get an NoClassDefFoundError, when I try to access a servlet. I compiled it with "javac -cp /home/tomcat/j2ee/lib/j2ee.jar:/home/tomcat/mysql MyServlet.java". this displays no error.

But I can't access the servlet. it shows me that:

exception

javax.servlet.ServletException: Error allocating a servlet instance

root cause

java.lang.NoClassDefFoundError: javax/servlet/http/HttpServlet

all that happens only in one "webapps"-dircetory, all others are working fine. Even when I copy one servlet from another "webapp" to it, I get that error.

[595 byte] By [Spineshanka] at [2007-10-2 6:13:10]
# 1
Do you have anything like servlet-api.jar in your web-inf/lib directory? Or a jar file containing any of the Servlet classes?That could potentially throw a spanner in the works for that one web app.
evnafetsa at 2007-7-16 13:14:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
No, there aren't. I don't have the directory "lib" and there's no jar-file.Can be something wrong with my $CLASSPATH? I haven't set it yet.
Spineshanka at 2007-7-16 13:14:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
You should have a servlet-api.jar in the $TOMCAT_HOME/common/lib directory IIRC. It contains the servlet api. You could also check if the j2ee.jar you specified for compilation exists (and has the necessary classes).
annie79a at 2007-7-16 13:14:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Actually, you should not be using the j2ee.jar in your case. Use the servlet-api.jar provided with Tomcat.
annie79a at 2007-7-16 13:14:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
that doesn't work. I get the same error as with j2ee.jar.Rarely, really rarely it works, but when I compile my source, without any changes, again, it doesn't work.
Spineshanka at 2007-7-16 13:14:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Please use servlet-api.jar which is bundled with tomcat in the classpath: egjavac -classpath /home/tomcat/servlet-api.jar /home/tomcat/mysql/MyServlet.java
KentStateUniversitya at 2007-7-16 13:14:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
can somebody help?i installed apache-tomcat 5.5.12 on linux.when i run the url for the servlet, error 404 appears.. requested url not found on this server.. can u tell me how to overcome this?thanksjade
Jadea at 2007-7-16 13:14:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
Did I type "j2ee.jar"? I meant servlet-api.jar, sorry.Can be something wrong with the startup-script of tomcat?
Spineshanka at 2007-7-16 13:14:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
1. What is your deployment directory Structure?2. Did your compilation cause error?3. What is inside your tomcat-server logs4. Did you import javax.*..*HttpServlet* in your sourcefile?5. How are your trying to access it?
KentStateUniversitya at 2007-7-16 13:14:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

1. I don't know, what you mean with "deployment directory". I guess it's the webapp-directory. Right? The structure of that is:

db

--WEB-INF

--web.xml

--classes

Servlet.java

Servlet.class

2. No, it doesn't

3. You can finde the whole entry for it [url=http://nopaste.debianforum.de/1705]here[/url]

4. I import it with

import javax.servlet.http.*;

5. My servlet? With "http://localhost:808/db/phone (I entered "phone" in web.xml as servlet-mapping)

Spineshanka at 2007-7-16 13:14:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11

Your servlet needs to be in a package.

As of Java 1.4, classes in the "unnamed" packaged are not accessible.

So your java file should be in

/WEB-INF/classes/mypackage/Servlet.java

Your web.xml file would need to be updated to match the new full class/package name.

However that doesn't explain your original error message.

Try compiling it from the WEB-INF/classes directory.with

"javac -cp .;/home/tomcat/common/lib/servlet-api.jar;/home/tomcat/mysql mypackage.MyServlet.java".

evnafetsa at 2007-7-16 13:14:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12

The error indicate that your class file is not found by servlet container. I guess your directory structure looks kinda weird. What you need to do is

1. replace db with Servlets

2. create a package "test" and store your file under $CATALINA_HOME/webapps/Servlets/WEB-INF/classes/test/Servlet.java

3. Edit your web.xml

<servlet>

<servlet-name>Servlet</servlet-name>

<servlet-class>test.Servlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Servlet</servlet-name>

<url-pattern>*.try</url-pattern>

</servlet-mapping>

4. Access by http://localhost:8080/Servlets/first.try

That should work

KentStateUniversitya at 2007-7-16 13:14:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 13

OK, I found the problem: When I started my tomcat-server with something like "path/dirs/startup.sh" I got that error.

Now, when I start it with "cd path/dirs/; ./startup.sh" It works just fine. Don't know why.

BTW: why do I have to use packages? It also works fine without any packages. I use JDK 1.5

Spineshanka at 2007-7-16 13:14:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 14

I encounter the same problem.

I have tested that put the class into a package is useless.

I defined one servet in package and another none. if I restart tomcat with full path like "/usr/local/apache-tomcat-5.5.20/bin/shutdown.sh && /usr/local/apache-tomcat-5.5.20/bin/startup.sh " , tomcat will throw the error , HttpServlet not found. But if I do a restart with " cd /usr/local/apache-tomcat-5.5.20/ && ./shutdown.sh && ./startup.sh" ,

everything will work fine.

If somebody can teach us what is class loading ?

arpcara at 2007-7-16 13:14:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...