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.
|
You will use the Java compiler javac to compile a Java program and
the Java interpreter java to execute it.
- Follow these instructions for installing a programming environment on Windows if you have not already done so.
|
You will type commands in an application called the Command Prompt.
-
The installer creates a Shortcut on the desktop to the Command Prompt.
Double-click it to launch the Command Prompt.
You should see something like:
Microsoft Windows [Version 6.3.9600] (c) 2013 Microsoft Corporation. All rights reserved.
-
To confirm that you have the right version of the Java compiler installed,
type the command in boldface below and check that the output matches:
C:\Users\username>javac -version javac 1.6.0_67
-
To confirm that you have the right version of the Java interpreter installed, type
the command in boldface below and check that the output matches:
C:\Users\username>java -version java version "1.6.0_67" Java(TM) SE Runtime Environment (build 1.6.0_67-b07) Java HotSpot(TM) 64-Bit Server VM (build 20.2-b06, mixed mode)
Java HotSpot(TM) Client VM (build 20.2-b06, mixed mode, sharing)
|
You will use the javac command to convert your Java program into a form more amenable for execution on a computer.
-
From the Command Prompt, navigate to the directory containing HelloWorld.java,
say C:\Users\username\introcs\hello, by typing the cd (change directory) commands below:
C:\Users\username>cd introcs C:\Users\username\introcs>cd hello C:\Users\username\introcs\hello>
-
Compile it by typing the javac command below:
C:\Users\username\introcs\hello>javac HelloWorld.java C:\Users\username\introcs\hello>
|
You will use the java command to execute your program.
-
From the Command Prompt, type the java command below.
C:\Users\username\introcs\hello>java HelloWorld Hello, World
|
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:
The PATH environment variable should include an entry for C:\Users\username\introcs\java\jdk\bin.
C:\Users\username> echo %PATH%
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.jarIf so, it is likely an issue with the Windows CLASSPATH environment variable. From the Command Prompt, type the following command to display it:
The CLASSPATH environment variable should include an entry for C:\Users\username\introcs\stdlib.jar.
C:\Users\username> echo %CLASSPATH%
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.