简体   繁体   中英

Eclipse: Why classes created manually aren't recognized?

For example: I'm making an Web project. At some line of one servlet, I put the following line:

ContatoDao dao = new ContatoDao();

So Eclipe complains that ContatoDao can't be found. 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). 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. Why?

From the Eclipse Project Explorer, right click on the Project and click on 'Properties'. Then click on 'Java Build Path' to inspect the 'Source' tab to see the 'Source folders on the build path'. 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:

import toppackage.subpackage.ContatoDao;

no matter which class you are importing it from (even a class in toppackage or toppackage.othersubpackage ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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