您如何让 Eclipse 识别 Java EE jar 文件以便 Servlet 可以编译?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/207541/
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
How do you make Eclipse recognize Java EE jar files so Servlets can compile?
提问by Quadir
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.
我正在设置我的 Java EE 版本的 Eclipse 来编译 Servlet。我遇到了 Eclipse 说“HttpServlet”无法解决的问题,因为它找不到 Java EE jar 文件。
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.
我使用的是 Windows XP。我已经启动并运行了 Tomcat 6.0。我认为最简单的解决方案是链接到 Tomcat 安装中的 servlet-api.jar 文件。
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
我将它添加到 Windows CLASSPATH 环境变量中。现在看起来像这样:.;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?
最好的解决方案是什么?
回答by Tatu Lahtela
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.
您可以将 servlet-api.jar 文件添加到项目属性中。在项目层次结构中右键单击项目名称->属性->Java 构建路径->库选项卡。从那里,将 servlet-api.jar 添加为外部 jar 文件。
回答by Quadir
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.
首先通过转到 New -> Server -> Server 添加 Tomcat Server。然后右键单击您的项目,单击 Build Path -> Configure Build Path。单击 Add Library -> Server Runtime 并选择您添加的 Tomcat Server。
回答by VonC
a few thing to check:
要检查的几件事:
Did you create your project as a "Dynamic Web Project" (via New->Project...->Web->Dynamic Web Project) ?
您是否将项目创建为“动态 Web 项目”(通过 New->Project...->Web->Dynamic Web Project)?
you should have a Web App Librairies directory which does contains:
您应该有一个 Web App Librairies 目录,其中包含:
- Web App Librairies
- JRE System Library
- Apache Tomcat vx.y
- 网络应用程序库
- JRE 系统库
- Apache Tomcat vx.y
Did you select a server in "Window -> Preferences... -> Server -> Installed Runtimes" ?
您是否在“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.
你在 Prefereces -> Java -> Build Path -> Classpath 变量中是否有一个 TOMCAT_HOME 引用你的 tomcat 安装的主目录?尝试将其添加到您的项目类路径中。
回答by nitind
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.
Eclipse 不使用 CLASSPATH 环境变量或任何其他环境变量(除非可能找到要运行的 VM)。这样做会使从同一台机器和安装中干净地定位不同的 VM 和服务器变得困难,如果不是不可能的话。使用的 jar 列表完全基于项目的 Java 构建路径。
回答by johnstok
For a regular java project you can do:
对于常规的 Java 项目,您可以执行以下操作:
- Create a folder in you eclipse project called 'lib'.
- Add the servlet jar to the lib folder.
- Right click on the servlet jar file in the Navigator view and choose "Add to build path".
- 在 eclipse 项目中创建一个名为“lib”的文件夹。
- 将 servlet jar 添加到 lib 文件夹。
- 在导航器视图中右键单击 servlet jar 文件并选择“添加到构建路径”。
For more complex projects I would suggest Maven+m2eclipse.
对于更复杂的项目,我建议使用 Maven+m2eclipse。