Monday, August 8, 2011

Solving Package javax.servlet does not exist Error using Eclipse and Ant

Suppose you are working on a web application project with Eclipse IDE, Apache ant and Tomcat server. When you compile and run the program you will get the following error saying that Servlet.jar API cannot be found.

Package  javax.servlet  does not exist

This error will get even if the servlet.jar API is available in the lib directory of the Tomcat web container. The reason for this error is you have not set the CLASSPATH environmental variable for referring javax.servlet API. In addition you have not set the Apache Ant run time class path to javax.servlet API.
You can follow the instructions given below to set both the classpaths properly.

1. Setting up CLASSPATH environmental variable.

 Right Click on My Computer -> Properties -> Advanced tab -> Environment Variables
(In that you can amend/create CLASSPATH environment variable to servlet.jar
(In my case, CLASSPATH=C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/javax.servlet.jar )

2. Setting up Apache Ant runtime classpath

In addition, it is required to include the javax.servlet API (servlet.jar ) into the Apache Ant runtime. This can be done using your Eclipse IDE as follows.
Click on Window menu -> preferences -> Ant -> Runtime -> Global Entries
(In this location, you can add the servlet.jar API to the Apache Ant runtime as an external JAR file).

if you follow the above instructions properly,  you should be able to compile and deploy your web application without having such compilation error.

1 comment:

  1. Setting up Apache Ant runtime classpath solved the issue. Thanks.