Y Pgjn
Y Pgjn

Reputation:

How do you make Eclipse recognize Java EE jar files so Servlets can compile?

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 Java EE 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?

Upvotes: 5

Views: 34973

Answers (5)

Quadir
Quadir

Reputation: 271

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.

Upvotes: 6

johnstok
johnstok

Reputation: 98300

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.

Upvotes: 1

nitind
nitind

Reputation: 20033

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.

Upvotes: 1

Tatu Lahtela
Tatu Lahtela

Reputation: 4564

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.

Upvotes: 7

VonC
VonC

Reputation: 1329512

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.

Upvotes: 2

Related Questions