Import Jar file into eclipse fails - cannot be res

2019-09-12 06:48发布

问题:

I imported some JARs successfully into my eclipse java project. I get "cannot be resolved to a type" error when I try to use the classes in those JAR files in my code.

Now I have to either use eclipse's auto help feature or type the import statements manually to kill the errors and use those classes.

How do i stop getting the errors and remove the need to import manually all the time. Eclipse never asks me to import Java.Lang.String. So, how do i ensure that it does the same thing for any other class ?

回答1:

You can use Eclipse shortcut CTRL+SHIFT+O to "organize imports" so Eclipse will add the import necessary automatically looking for the class needed into the classpath. It can save you time, it is faster than going one by one using Ctrl-Space shortcut on an unrecognized class.



回答2:

You don't. You'll have to import them all, but you can do it per-package, rather than per-Class.

All classes in java.lang are automatically imported into every Java program. Likewise with all classes in the same package as the class containing the main() function. Other than that, you have to import each class.

Eclipse will probably offer to import an unrecognized class for you - try the Ctrl-Space shortcut on an unrecognized class.

Finally, it won't hurt to read this: http://en.wikipedia.org/wiki/Java_package - you may already all this, but there it is just in case.