Reputation: 1668
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
?
Upvotes: 6
Views: 11002
Reputation: 11
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
Upvotes: 1
Reputation: 11
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.
Upvotes: 1
Reputation: 1688
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.
Upvotes: 3