Standard Libraries
Here is a list of the input and output libraries that we use throughout
the textbook and beyond.
The file stdlib.jar bundles together all of our
standard libraries into one file. The easiest way to use is to unjar them
in your current working directory.
% jar xf stdlib.jar
We briefly describe the input and output libraries and include a sample client.
Standard input and standard output.
StdIn.java and StdOut.java are libraries for reading in numbers and text from standard input and printing out numbers and text to standard output. Our versions have a simpler interface than the corresponding Java ones (and provide a few tecnical improvements). Average.java reads in a sequence of real numbers from standard input and prints their average on standard output.In.java and Out.java are object-oriented versions that support multiple input and output streams, including reading from a file or URL and writing to a file. Wget.java reads in data from the URL specified on the command line and save it in a file with the same name.
% java Average 10.0 5.0 6.0 3.0 7.0 32.0 3.14 6.67 17.71 <Ctrl-d> Average is 10.05777777777778
% java Wget http://www.cs.princeton.edu/IntroProgramming/datafiles/codes.csv % more codes.csv United States,USA,00 Alabama,AL,01 Alaska,AK,02 ...
Binary standard input and standard output.
BinaryStdIn.java and BinaryStdOut.java are the analogs for binary data. Copy.java reads a binary file from standard input and writes it to standard output.
% java Copy < mandrill.jpg > copy.jpg % diff mandrill.jpg copy.jpg
Standard drawing.
StdDraw.java is an easy-to-use library for drawing geometric shapes, such as points, lines, and circles. RightTriangle.java draws a right triangle and a circumscribing circle.
% java RightTriangle
![]()
BouncingBall.java illustrates how to produce an animation using standard drawing.
Your browser can not display this movie.
Be sure that Javascript is enabled and that you have Flash 9.0.124 or better.
Draw.java is an object-oriented
versions that support drawing in multiple windows.
Standard audio.
StdAudio.java is an easy-to-use library for synthesizing sound. Tone.java reads in a frequency and duration from the command line, and it sonifies a sine wave of the given frequency for the given duration.
% java Tone 440.0 3.0
Image processing.
Picture.java is an easy-to-use library for image processing. Scale.java takes the name of a picture file and two integers (width w and height h) as command-line arguments and scales the image to w-by-h.
|
|
|
Using the standard libraries in your programs.
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.
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?
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.