简体   繁体   English

Eclipse:为什么无法识别手动创建的类?

[英]Eclipse: Why classes created manually aren't recognized?

For example: I'm making an Web project. 例如:我正在制作一个Web项目。 At some line of one servlet, I put the following line: 在一个servlet的某一行中,我输入了以下行:

ContatoDao dao = new ContatoDao();

So Eclipe complains that ContatoDao can't be found. 因此,Eclipe抱怨ContatoDao And it's right; 是的。 this class is at another package. 此类在另一个包中。 So, in order to test, I copied contatoDao to the same directory of the present class (also changing it's package to the same package of the present class). 因此,为了进行测试,我将contatoDao复制到了当前类的同一目录(也将其包更改为当前类的同一个包)。 But I keep getting this error: 但我不断收到此错误:

HTTP Status 500 - Servlet execution threw an exception


  (...)


root cause
java.lang.Error: Unresolved compilation problems: 
    ContatoDao cannot be resolved to a type
    ContatoDao cannot be resolved to a type

Which doesn't occurs if I create the same class, at the same place, but using the create class of Eclipe. 如果我在同一地点使用Eclipe的create类创建相同的类, 则不会发生这种情况 Why? 为什么?

From the Eclipse Project Explorer, right click on the Project and click on 'Properties'. 在Eclipse Project Explorer中,右键单击Project,然后单击“ Properties”。 Then click on 'Java Build Path' to inspect the 'Source' tab to see the 'Source folders on the build path'. 然后单击“ Java构建路径”以检查“源”选项卡,以查看“构建路径上的源文件夹”。 Ensure that the folder where your source code resides is listed. 确保列出了您的源代码所在的文件夹。

Furthermore, also inspect the 'Order and Export' tab and ensure that the folder at the root of the package hierarchy is listed and selected here. 此外,还要检查“订购和出口”标签,并确保在此处列出并选择了包层次结构根目录下的文件夹。

Imports are not, and cannot be, relative to the current package. 相对于当前软件包而言,导入不是,也不能是。

If you have a top-level package toppackage , containing a package subpackage , containing a class ContatoDao , then to import ContatoDao you must use: 如果您有一个顶级软件包toppackage ,其中包含package subpackage ,其中包含类ContatoDao ,那么要导入ContatoDao您必须使用:

import toppackage.subpackage.ContatoDao;

no matter which class you are importing it from (even a class in toppackage or toppackage.othersubpackage ). 无论您从哪个类导入它 (甚至是toppackagetoppackage.othersubpackage的类)。

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

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