Introduction to Programming in Java: Standard Libraries
Input and output libraries.
Here is a list of the input and output libraries that we use throughout the textbook.
The file stdlib.jar bundles together all of the I/O libraries above into a .jar file.
Accessing the libraries.
There are a number of ways to access the libraries:- Current directory.
The simplest method is to put the desired .java file
(such as StdIn.java)
in the same directory as the program you are writing.
Then, compile and execute as usual.
% javac MyProgram.java % java MyProgram
- Classpath.
The file stdlib.jar bundles together all of our
standard libraries into one file.
To access it, put stdlib.jar in the same directory as
the program you are writing.
Then, compile and execute as follows.
The -cp flag sets the classpath. The . tells Java to look in the current directory for .java and .class files (such as MyProgram.java and MyProgram.class). The stdlib.jar tells Java to also look in the .jar file. On OS X, the : separates directories in the classpath; on Windows the ; separates directories.OS X / Linux ------------ % javac -cp .:stdlib.jar MyProgram.java % java -cp .:stdlib.jar MyProgram Windows ------------ % javac -cp .;stdlib.jar MyProgram.java % java -cp .;stdlib.jar MyProgram
- Configure your IDE. You can configure your IDE to automatically include stdlib.jar in the classpath. In DrJava, it is Preferences -> Extra Classpath -> Add.
Q. If I use a named package to structure my code, the compiler can no longer access the libraries in stdlib.jar. Why not? The libraries in stdlib.jar are in the "default" package.
A. 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.
