Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing referenced libraries from packages in eclipse

Tags:

java

eclipse

jar

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?

enter image description here

like image 431
ddayan Avatar asked Sep 05 '12 17:09

ddayan


1 Answers

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.

like image 145
Jasmine Avatar answered Oct 24 '22 22:10

Jasmine