views:

6467

answers:

5

I am setting up my Java EE version of Eclipse to compile Servlets. I have the problem where Eclipse says "HttpServlet" cannot be resolved because it can't find the JEE jar files.

I am using Windows XP. I already have Tomcat 6.0 up and running. I think the easiest solution would be to link to the servlet-api.jar file in the Tomcat installation.

I added it to the Windows CLASSPATH environment variable. Now it looks like this: .;C:\Program Files\Java\jre1.6.0_04\lib\ext\QTJava.zip;C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar

That didn't work. Am I doing something wrong?

What is the best solution?

+1  A: 

a few thing to check:

Did you create your project as a "Dynamic Web Project" (via New->Project...->Web->Dynamic Web Project) ?

you should have a Web App Librairies directory which does contains:

  • Web App Librairies
  • JRE System Library
  • Apache Tomcat vx.y

Did you select a server in "Window -> Preferences... -> Server -> Installed Runtimes" ?

Do you have a TOMCAT_HOME in Prefereces -> Java -> Build Path -> Classpath variable referring to the home directory of your tomcat installation ? Try to add it to your project classpath.

VonC
+1  A: 

You can add the servlet-api.jar file into the project properties. From the project hierarchy right click the project name->properties->Java build path->Libraries tab. From there, add the servlet-api.jar as an external jar file.

Tatu Lahtela
Download the servlet-api.jar from the following URL http://www.java2s.com/Code/Jar/STUVWXYZ/Downloadservletapijar.htmThis will do
harigm
+1  A: 

Eclipse doesn't make use of the CLASSPATH environmental variable, or any other (except maybe to find a VM to run). Doing so would make it difficult if not impossible to cleanly target different VMs and servers from the same machine and installation. The list of jars used is solely based on the project's Java Build Path.

nitind
A: 

For a regular java project you can do:

  1. Create a folder in you eclipse project called 'lib'.
  2. Add the servlet jar to the lib folder.
  3. Right click on the servlet jar file in the Navigator view and choose "Add to build path".

For more complex projects I would suggest Maven+m2eclipse.

johnstok
A: 

First add Tomcat Server by going to New -> Server -> Server. Then right-click on your project, click on Build Path -> Configure Build Path. Click on Add Library -> Server Runtime and Choose the Tomcat Server you added.

Quadir