javax.servlet cannot be resolved - how to solve
Archive - Originally posted on "The Horse's Mouth" - 2008-11-26 08:03:07 - Graham EllisTwo common problems in compiling Servlets for use within a Tomcat container ...
PROBLEM: My Java Compile of a Servlet says "javax.servlet cannot be resolved"
For example:
[trainee@easterton sources]$ javac Smallest.java
----------
1. ERROR in Smallest.java
(at line 2)
import javax.servlet.*;
^^^^^^^^^^^^^
The import javax.servlet cannot be resolved
----------
2. ERROR in Smallest.java
(at line 3)
import javax.servlet.http.*;
^^^^^^^^^^^^^
The import javax.servlet cannot be resolved
----------
3. ERROR in Smallest.java
(at line 5)
public class Smallest extends HttpServlet {
^^^^^^^^^^^
HttpServlet cannot be resolved to a type
----------
4. ERROR in Smallest.java
(at line 9)
public void doGet(HttpServletRequest request,
^^^^^^^^^^^^^^^^^^
HttpServletRequest cannot be resolved to a type
----------
5. ERROR in Smallest.java
(at line 10)
HttpServletResponse response)
^^^^^^^^^^^^^^^^^^^
HttpServletResponse cannot be resolved to a type
----------
6. ERROR in Smallest.java
(at line 11)
throws IOException, ServletException
^^^^^^^^^^^^^^^^
ServletException cannot be resolved to a type
----------
6 problems (6 errors)[trainee@easterton sources]$
SOLUTION: Add the Servlet API to your CLASSPATH.
[trainee@easterton sources]$ export CLASSPATH=/usr/local/tomcat/common/lib/servlet-api.jar
[trainee@easterton sources]$ javac Smallest.java
[trainee@easterton sources]$
EXPLANATION:
Even if you're just compiling a Servlet to produce a class file, on a machine on which you won't actually be running it, the Java compiler insists on having the necessary classes available to check you code / to know what it is extending.
The Servelet API jar file is available from within a standard Tomcat download, and also from within a Java Enterprise download, but it is not downloaded with the standard edition!
PROBLEM: I get an error message about "does not declare serialVersionUID"
[trainee@easterton sources]$ javac Smallest.java----------
1. WARNING in Smallest.java
(at line 5)
public class Smallest extends HttpServlet {
^^^^^^^^
The serializable class Smallest does not declare a static final serialVersionUID field of type long
----------
[trainee@easterton sources]$
SOLUTION: Check that you're using the correct Java compiler!
1 problem (1 warning)[trainee@easterton sources]$ which java
/usr/bin/java
[trainee@easterton sources]$ export PATH=/usr/local/java/bin:$PATH
[trainee@easterton sources]$ javac Smallest.java
[trainee@easterton sources]$
EXPLANATION: There are various Java Compilers available, and the one that's shipped with Linux releases is highly unlikely bo be Sun's one which you'll want to use with Servlets / Tomcat's classes.
If you have Tomcat downloaded and installed already, chances are that you do have the right compiler already on your system - if not, download it from Sun - but that your PATH to executable files is currently pointing at the one shipped with the operating system.
Learn all about these on our Deploying Apache httpd and Tomcat course!