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:
- Open the System Properties (Windows+Break)
- Click the Advanced Tab and open Environment Variables
- Under System Variables, edit the CLASSPATH field
- 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;
- 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: http://www.sitepoint.com/article/java-servlets-1/3/
…You’ve been served