Why does import javax.servlet.*; cannot be resolve

2019-03-27 06:42发布

问题:

This question already has an answer here:

  • How do I import the javax.servlet API in my Eclipse project? 14 answers

The open source project which I want to use needs to import:

import javax.servlet.*;
import javax.servlet.http.*;

As far as I know those libraries are part of the Java EE. So I installed Java EE from this location: http://www.oracle.com/technetwork/java/javaee/downloads/java-ee-sdk-7-jdk-7u21-downloads-1956231.html . After installing Java EE and rebooting I still get information about unresolved imports. What should I do to make those errors disappear?

回答1:

please cross check if your eclipse is pointing to JDK and not JRE

check in build path, it has to point to JDK change if it is pointing to JRE



回答2:

Rather than downloading the whole of Java EE, you may be best to download just the Servlet API and then add that to your Eclipse classpath. You can get hold of the jar file here

Then it's just a case of adding the library to your Eclipse classpath by right-clicking your 'Camera' project and navigating to 'Properties > Java Build Path > Libraries'. From there you can point to the downloaded jar file (Add External JARs).



回答3:

Have you properly set the CLASSPATH variable to point to your libraries ?

I remember when I was doing Servlets using notepad, I always used to set the -cp of javac. I am no expert in this field though. =)



回答4:

You will have to update the classpath of the project that uses the javax.servlet.* classes in Eclipse with the necessary jars containing the javax.servlet and related packages.

So select the project (left pane), right click → Build Path → Configure Build Path. Go to the "Libraries" tab, select "Add JARs" and navigate to the JAR(s) containing the packages you want. These will probably be in the installation of the EE SDK (you will have to search a bit).

BUT...

You will probably have better luck installing Eclipse for JEE developers (currently here). It will open by default in a new Perspective (the icons on the top-right of the toolbar): JEE instead of Java.

Then create a server of the type you are using (for plain web projects, Tomcat would suffice): Window → Preferences → Server → Runtime Environemnts → Add. You will have to download the runtime for your server, if not provided (Tomcat is provided) and select the installation directory.

Then create a new server with this runtime: Window → Show View → Other → Servers. In the opened "Servers" view, right click and select New → Server. Give it a name and the Runtime environment you created above.

Now create a new Web Project and assign it to the newly created server. From the servers view, you will be able to start and debug the application in the server, redeploy it etc.


These are quick instructions... If you are completely new to this kind of development, maybe you should seek additional help for topics that trouble you. ALSO you may want to experiment with a simple project (a servlet, a filter, a JSP etc) first to get acquianted with the tools.