简体   繁体   中英

accessing referenced libraries from packages in eclipse

I can only access referenced library classes if I save my classes in the default package. If I try to access them from any other package I get "className cannot be resolved". Any idea why this could happen?

在此输入图像描述

That package is from the standard library of Princeton's IntroCS Course after a quick Google.

If you follow down to the FAQ on the page http://introcs.cs.princeton.edu/java/stdlib/

Q. If I use a named package to structure my code, the compiler can no longer access the libraries in stdlib.jar. Why not? A. The libraries in stdlib.jar are in the "default" package. In Java, you can't access classes in the default package from a named package. If you need to use our libraries with a named package, you can use the packaged version stdlib-package.jar.

Download package jar file: http://introcs.cs.princeton.edu/java/stdlib/stdlib-package.jar

Right click project folder and add external JAR.

import edu.princeton.cs.introcs.*;

Add above line to the classes where you need to reference the classes. The first line references the correct package name and the * wildcard imports all the classes within it.

:) Hope that helps.

//Edit - If you right click the project folder and use "Organize Imports" it will be faster so you don't have to manually add to each class.

Check if you've proper import declarations and the type you are refering to has public access modifier.

Import declaration for Types in the default package are per definition impossible. JLS specifies that it is a compile time error to import a type from an unnamed package. You must access your class via reflections or much better do not ever use the default package. Eclipse should show you a warning when you want to create a type inside default package, because it's generally discouraged.

If you're using an IDE like Eclipse then try hitting CTRL + SPACE behind the type name in the class where you want to use it. Eclipse should give you all matching opportunities and will add the import automatically for you if you select your class.

You need to import the package into other package.

Classes in one package cannot directly reference to other package unless its get referenced.

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