简体   繁体   English

Eclipse:无法识别导入

[英]Eclipse: Imports not being recognized

I'm using in my Eclipse (Java EE edition) project some classes, like the ServletConfig , for example. 我在Eclipse(Java EE版)项目中使用了一些类,例如ServletConfig If I just add some code with an object of this class without importing it, and click on the error sign (where should appear the import option), it only shows the option to create a class with that name. 如果我只是添加一些带有此类对象的代码而不导入它,然后单击错误符号(应该在其中显示导入选项),则它仅显示使用该名称创建类的选项。 And if I import it anyway: 如果我仍然导入它:

import javax.servlet.ServletConfig;

It points an error and shows me the option to create the class 'ServletConfig' in packgae 'javax.servler' . 它指出了一个错误,并向我显示了create the class 'ServletConfig' in packgae 'javax.servler'的选项。 It was working on another computer with Windows 7. The one I'm using is Windows 8.1. 它正在另一台装有Windows 7的计算机上工作。我正在使用的是Windows 8.1。

Help! 救命!

It sounds as if your project isn't configure as a web project but rather as a plain Java project. 听起来好像您的项目没有配置为Web项目,而是配置为纯Java项目。 Usually, you would do it when you create a project. 通常,在创建项目时会这样做。 You'd select File / New / Dynamic Web Project . 您将选择File / New / Dynamic Web Project

If you haven't done so, you should be able the fix the configuration. 如果尚未这样做,则应该可以修复配置。 In the project settings, go to Project Facets and check Dynamic Web Module . 在项目设置中,转到Project Facets并检查Dynamic Web Module

在此处输入图片说明

If I'm not mistaken, you'll need to set up a web server runtime environment as well. 如果我没记错的话,您还需要设置一个Web服务器运行时环境。 First go to Eclipse's preferences and select Server / Runtime Environments . 首先转到Eclipse的首选项,然后选择服务器 / 运行时环境 If the list is empty, add one. 如果列表为空,请添加一个。

在此处输入图片说明

Then go to the project settings again and make sure there is a targeted runtime selected: 然后再次转到项目设置,并确保选择了目标运行时:

在此处输入图片说明

If everything goes ok, the project explorer will show all the web server jars in Java Resources / Libraries / Apache Tomcat v7.0 (or whatever your environment is called). 如果一切正常,项目浏览器将显示Java资源 / / Apache Tomcat v7.0 (或您所称的环境)中的所有Web服务器jar。 One of the jars is servlet-api.jar and contains the classes you're looking for. jar之一是servlet-api.jar,其中包含您要查找的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM