Servlet not found!

I just wasted about 2 hours trying to figure out why my java servet won’t compile.  The answer was actually very simple, so I want to save people that time, which I wasted.

The error:

package javax.servlet does not exist
import javax.servlet.*;
1 error

This caused about 10 other errors, all related.  You must include in the CLASSPATH the .jar file which contains the servlet class.  Unfortunately I included the wrong .jar file.  I was using the javaee.jar file in ‘C:\Sun\SDK\lib\‘, which while it does have the classes, does not work.  I am using a Tomcat server for my servlets, and in the Tomcat installation directory is the d servlet-api.jar, which also contains the servlet classes.

The Answer:

  1. Open the System Properties (Windows+Break)
  2. Click the Advanced Tab and open Environment Variables
  3. Under System Variables, edit the CLASSPATH field
  4. If there is already data in there then add a ‘;’ at the end of the line and then the location of servlet-api.jar.  If you are using Tomcat and used the default installation directory, then you should be entering: C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar;
  5. Ok all the changes and restart whatever app you are using to compile, so that the new Environment Variables can be loaded.

Here is my reference:

…You’ve been served

Leave a Reply

Your email address will not be published. Required fields are marked *