I have a jar file named "stdlib.jar"
. This has many classes in its "Default Package"
. I have added this stdlib.jar
to my NetBeans Libraries. and Also "Build"-ed
it without using anything from stdlib.jar
. So that stdlib.jar
can be added to my "./dist/lib"
folder. But still I can't use any classes within stdlib.jar
What should I do to reuse any classes within stdlib.jar
?
3 Answers
Using "Default Package" package for libraries or even for a project is highly discouraged.
If you use default package then the classes name should be unique or else they will override all the java.lang
classes.
Now coming to your situation. To use your stdlib.jar
if you've added it in your library then you should be able to use it.
You can use your stdlib.jar
from your default package of the project in which you are using it. Other than default package you cannot use it as it will search your stdlib.jar
class in that package.
Hope this makes you clear. Thanks.

- 1,672
- 14
- 17
-
But it didn't solve my problem. I have added "stdlib.jar" to Libraries. and tried to access a class within it. It's unable to find that class. What can I do? Please suggest some way. I know it can be done by just extracting the class from the jar and then adding that java file to the source. but I don't want to do that. Because I may need other classes of the "stdlib.jar" also." – soham Aug 22 '12 at 06:50
-
1if you have classes in default package, you can access them from default package only. you cannot do anything else.. – pratikabu Aug 22 '12 at 06:58
-
1excellent answer, bulls eye. I was banging my head against the wall before looking at this answer :) – Kamran Ali Feb 05 '13 at 15:17
You are using the Sedgewick's textbook, aren't you? If you are, read the FAQ:
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
Same question here: Getting a library to import properly in netbeans

- 1
- 1

- 11
- 1
If you are using the Sedgewick textbook or Algorithms course on Coursera, this may be of help.
As it was mentioned above, 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. So you have to move your source files to a DEFAULT PACKAGE of your project. It means that you have to move your source .java file(s) from the ./src/YOURPACKAGE/ folder directly to the ./src folder of your project, then delete the YOURPACKAGE folder. Also you have to delete the "package YOURPACKAGE;" statement at the beginning of your source file(s).
Basically that's all you need to do. Now when you moved your source file(s) to the default package, and added "stdlib.jar" to Libraries, you would have no problem addressing the classes from the library. You don't need no "import" statment for them.

- 11
- 1