为什么导入javax.servlet.*; 尽管安装了 Java EE 仍无法解决 | 面向 Java EE 开发人员的 Eclipse
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20196936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does import javax.servlet.*; cannot be resolved despite of installing Java EE | Eclipse for Java EE Developers
提问by Yoda
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?
据我所知,这些库是 Java EE 的一部分。所以我从这个位置安装了 Java EE:http: //www.oracle.com/technetwork/java/javaee/downloads/java-ee-sdk-7-jdk-7u21-downloads-1956231.html。安装 Java EE 并重新启动后,我仍然会收到有关未解析导入的信息。我该怎么做才能使这些错误消失?
采纳答案by dev2d
please cross check if your eclipse is pointing to JDK and not JRE
请交叉检查您的 Eclipse 是否指向 JDK 而不是 JRE
check in build path, it has to point to JDK change if it is pointing to JRE
检查构建路径,如果它指向 JRE,它必须指向 JDK 更改
回答by Little Child
Have you properly set the CLASSPATH
variable to point to your libraries ?
您是否正确设置了CLASSPATH
变量以指向您的库?
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. =)
记得我在Servlets
用记事本做的时候,一直都是设置-cp
javac的。虽然我不是这个领域的专家。=)
回答by Will Keeling
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
与其下载整个 Java EE,不如只下载 Servlet API,然后将其添加到 Eclipse 类路径中。您可以在此处获取 jar 文件
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).
然后,只需通过右键单击“相机”项目并导航到“属性 > Java 构建路径 > 库”,将库添加到 Eclipse 类路径即可。从那里您可以指向下载的 jar 文件(添加外部 JAR)。
回答by Nikos Paraskevopoulos
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.
您必须使用javax.servlet.*
包含 Eclipsejavax.servlet
和相关包的必要 jar更新使用Eclipse 中的类的项目的类路径。
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).
所以选择项目(左窗格),右键单击→构建路径→配置构建路径。转到“库”选项卡,选择“添加 JAR”并导航到包含所需包的 JAR。这些可能会在 EE SDK 的安装中(您必须搜索一下)。
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.
为 JEE 开发人员安装 Eclipse 可能会更好(目前在这里)。默认情况下,它将在新的 Perspective(工具栏右上角的图标)中打开:JEE 而不是 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.
然后创建您正在使用的类型的服务器(对于普通 Web 项目,Tomcat 就足够了):窗口 → 首选项 → 服务器 → 运行时环境 → 添加。如果未提供(提供 Tomcat),则必须为您的服务器下载运行时并选择安装目录。
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.
现在创建一个新的 Web 项目并将其分配给新创建的服务器。从服务器视图,您将能够在服务器中启动和调试应用程序,重新部署它等。
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. ALSOyou may want to experiment with a simple project (a servlet, a filter, a JSP etc) first to get acquianted with the tools.
这些是快速说明...如果您对这种开发完全陌生,也许您应该为困扰您的主题寻求其他帮助。此外,您可能想先尝试一个简单的项目(servlet、过滤器、JSP 等)以熟悉这些工具。