Issue
So I want to mess around with a library I just downloaded called stdlib.jar
(downloaded from http://introcs.cs.princeton.edu/java/stdlib/)
So I added the library to the project, and when I inspect the project it looks like this in netbeans:
So it seems that the library is set up properly.
But then in my project when I try to use one of the library classes, it looks like this.
Mousing over the import statement at the top, says expected "."
And If I change the import statement to say import stlib.StdAudio
it says that the library does not exist.
Any body know what the problem is?
Solution
All the source code of this library (which is pretty unusual: first time I encountered this) is in the default package, which means you don't have to import anything if your class is also in the default package. Just use the class name.
StdAudio.play(tone);
Just make sure it is in the classpath, but I think it is if I look at your screenshot from NetBeans.
Update: It seems like I'm wrong. At the bottom of the page, you can find this:
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 standard libraries with a named package, you must put our libraries in a package, as in this solution.
So, you have to copy the classes into your project its package musicplayer
.
Answered By - Martijn Courteaux
Answer Checked By - Clifford M. (JavaFixing Volunteer)