Using Java from the Command Prompt in Windows


This document instructs you on how to use Java from the Command Prompt in Windows. The Command Prompt is necessary for redirecting standard input, redirecting standard output, and piping—you will use these features in Section 1.5.

These instructions apply to 32-bit and 64-bit Windows 8, Windows 7, Vista SP1, and XP SP3.


Install the Programming Environment


You will use the Java compiler javac to compile a Java program and the Java interpreter java to execute it.


Command-Line Interface


You will type commands in an application called the Command Prompt.


Compile the Program


You will use the javac command to convert your Java program into a form more amenable for execution on a computer.


Execute the Program


You will use the java command to execute your program.


Troubleshooting


Here are a few suggestions that might help correct any installation woes you are experiencing. If you need assistance, don't hesitate to contact a staff member.

I deleted the Shortcut on the desktop to the Command Prompt. Where can I find it? Go to All Programs -> Accessories -> Command Prompt.

When I type, "java -version" or "javac -version" I get an error. First, verify that the following two files exists:

C:\Users\username\introcs\java\jdk\bin\javac.exe
C:\Users\username\introcs\java\jdk\bin\java.exe

If so, it is likely an issue with the Windows PATH environment variable. From the Command Prompt, type the following command to display it:

C:\Users\username> echo %PATH%
The PATH environment variable should include an entry for C:\Users\username\introcs\java\jdk\bin.

I successfully compiled HelloWorld.java with javac, but, when I execute, I get the error message "Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld". What am I doing wrong? First, verify that the file HelloWorld.class is in the current directory. Be sure to type java HelloWorld without a trailing .class or .java.

When I compile or execute a program from the Command Prompt that uses StdIn, I get an error. How can I fix this? First, verify that the following file exists:

C:\Users\username\introcs\stdlib.jar
If so, it is likely an issue with the Windows CLASSPATH environment variable. From the Command Prompt, type the following command to display it:
C:\Users\username> echo %CLASSPATH%
The CLASSPATH environment variable should include an entry for C:\Users\username\introcs\stdlib.jar.

How do I break out of an infinite loop in the Command Prompt? Type Ctrl-c.

When using standard input, how do I signify that there is no more data? If you are entering input from the keyboard, type Ctrl-z for EOF (end of file). On some DOS systems the first line of output sent to the screen after you enter EOF will be rendered invisible by DOS. This is not a problem with your code, but rather a problem with DOS. To help you debug your program, we recommend including an extra System.out.println(); statement before what you really want to print out. If anyone knows of a better fix, please let us know!

How do I navigate to another drive from the Windows Command Prompt? From the Command Prompt, type H: to switch to the H: drive. Then, use the cd command to navigate to the desired directory.

Where can I learn more about Command Prompt? Microsoft maintains a command-line reference.

Can I use the Windows PowerShell instead of the Command Prompt? The Windows PowerShell is a more advanced command-line shell. However, it does not currently support the redirection of standard input.