This document instructs you on how to use the Windows XP Command Prompt with
Java.
Java |
You will use the Java compiler javac to compile your Java programs and the Java interpreter java to run them. You should skip the first step if Java is already installed on your machine.
[ In Vista, select Start -> Control Panel -> System and Maintenance -> System -> Advanced system settings -> Advanced -> Environment Variables -> PATH. ]
You might need to switch to Classic view instead of Category view to discover all of the options.
Command Line Interface |
You will type commands in an application called the Command Prompt.
Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp.
Then type:C:\Documents and Settings\wayne>java -version java version "1.5.0_16" Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_16-b05) Java HotSpot(TM) Client VM (build 1.5.0_16-b05, mixed mode, sharing)
C:\Documents and Settings\wayne>javac -version javac 1.5.0_16 javac: no source files ...
Click OK and check Save properties for future windows with same title to make the changes permanent.
Now you are ready to write your first Java program.
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } }
Compile the Program |
You will use the javac command to convert your Java program into a form more amenable for execution on a computer.
C:\Documents and Settings\pi1> cd c:\pi1\hello C:\pi1\hello\>
If everything went well, you should see no error messages.C:\pi1\hello\> javac HelloWorld.java C:\pi1\hello\>
Execute the Program |
You will use the java command to execute your program.
If all goes well, you should see the output of the program - Hello, World.C:\pi1\hello\>java HelloWorld Hello, World
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.
When I type, "java -version" I get an error. Check that you edited your PATH environment variable as indicated. A missing ; or an added % is enough to screw things up. Close and re-open a command prompt. Type path at the command prompt and look for an entry that includes C:\Program Files\Java\jdk1.5.0_16\bin;. Check that the version number 1.5.0_16 matches the one you installed since Sun updates Java periodically and you might have a more recent version. If this doesn't fix the problem, check if you have any old versions of Java on your system. If so, un-install them and re-install Java.
The command "java -version" works, but not "javac -version". Any thoughts? It's likely a path issue. Try the suggestions from the previous question. Also check that you installed the JDK properly by checking that the folder Program Files\Java\jdk1.5.0_16\bin exists.
How can I check the values of my PATH variable? Type the following at the command prompt.
The PATH variable should begin with C:\Program Files\Java\jdk1.5.0_16\bin;.C:\pi1\hello\> echo %PATH%
I can compile with javac, but I get the error message "Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld" when I try to execute it with java. First, be sure that HelloWorld.class is now in the current directory. Be sure to type java HelloWorld without a trailing .class or .java. Check that the command "java -version" works. Now try to execute with "java -cp . HelloWorld". If this works, you need to edit your classpath. (iTunes has a proclivity for changing the classpath, so if you recently upgraded iTunes, this is likely the source of the problem.). If you have problems, ask the teaching staff for help.
Where can I learn more about the Windows XP command line? Here is a short tutorial on the
Windows command prompt. Microsoft maintains a complete command line reference.