HOWTO · Java
StdDraw Java Import
Add Princeton's StdLib JAR to an Eclipse Java project, import StdDraw in source code, and troubleshoot classpath errors.
On this page
To use StdDraw in a Java project, download Princeton’s stdlib.jar, add it to the project’s build path, and import the class in your Java source. Adding a JAR to Eclipse and writing an import statement are separate steps: the first makes the library available to the compiler and runtime; the second lets your source refer to its class by the short name.
What StdDraw is and what you need
StdDraw is a drawing API in Princeton’s StdLib, a library used in introductory programming. It provides calls for drawing shapes and text and setting colors. StdLib is an external dependency; it is not included in the Java JDK or JRE. You need a copy of stdlib.jar and a Java project in Eclipse. The package used by this guide is edu.princeton.cs.introcs, as in the import below.
Download the StdLib JAR
Open Princeton’s StdLib page and locate stdlib.jar in the library listing. Download the JAR file itself. In your project, create a folder named lib if you want to keep dependencies alongside the source, then copy the downloaded file there. Keeping the JAR in the project makes its location easier to recognize; copying it alone does not add it to Eclipse’s build path.
The Princeton page’s library listing and stdlib.jar download context.
Add stdlib.jar to an Eclipse project
In Package Explorer, right-click the project and choose Properties. Open Java Build Path, select Libraries, then choose Add JARs… if the file is inside the project, or Add External JARs… if it is elsewhere. For the project lib folder, expand the project and select lib/stdlib.jar. Select Apply and Close. The JAR should now appear under the project’s classpath entries. If Eclipse presents an Order and Export tab, that tab controls what dependent projects receive; it is not where you add the JAR.
The Libraries view shows a JAR attached to the project’s classpath.
Use StdDraw in Java
After adding the JAR, import the class in the source file that uses it. For example, a drawing program can begin with import edu.princeton.cs.introcs.StdDraw; and then call StdDraw.setPenColor(StdDraw.BLUE); followed by StdDraw.filledCircle(0.5, 0.5, 0.1);. These calls set the pen color to blue and draw a filled circle centered at (0.5, 0.5) with radius 0.1 in StdDraw’s unit-square coordinate system. The import does not load the JAR; the project build path does that. Likewise, adding the JAR does not remove the need for the source import.
Use the following small Java program to check whether the class can be found on the running program’s classpath. It reports the lookup result without requiring a graphical window:
public class CheckStdDraw {
public static void main(String[] args) {
try {
Class.forName("edu.princeton.cs.introcs.StdDraw");
System.out.println("StdDraw is available on the classpath.");
} catch (ClassNotFoundException e) {
System.out.println("StdDraw is missing from the classpath.");
}
}
}
Troubleshoot common setup errors
If Eclipse reports that edu.princeton.cs.introcs does not exist or cannot resolve StdDraw, check that the JAR appears in Project Properties → Java Build Path → Libraries for the same project that contains your source. If it does not, add it again and apply the change. If Eclipse accepts the import but launching the program reports a missing class, check the launch configuration’s classpath as well as the project build path; compilation and launch must both include the dependency.
Check spelling and capitalization in the package and class names, and make sure the file you added is the downloaded JAR rather than a web page or archive containing a different library. If you move or rename the JAR, update its build-path entry. A Java import statement only names a class already available to the project; it cannot download or repair a missing dependency. Once both the build path and source import are correct, the drawing calls can use StdDraw directly.