This documents instructs you on how to setup a Java programming
environment for your Windows XP computer and provides a step-by-step
guide to creating, compiling, and executing a Java program.
Windows NT and Windows 2000
are similar, but you might need to make some minor adjustments.
All of the software is freely available on the Web.
| Java |
You will use the Java compiler javac to compile your Java programs and the Java interpreter java to run them.
| 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.
C:\Documents and Settings\wayne>java -version java version "1.4.2_03" Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_03-b02) Java HotSpot(TM) Client VM (build 1.4.2_03-b02, mixed mode) C:\Documents and Settings\wayne>
The mkdir command creates a new directory; the cd command changes the current working directory. After executing these commands, your working directory is the newly created C:\introcs\hello. All of your files for Assignment 0 will go here. Don't be scared by the Command Prompt - you will only need to use a few basic commands. Keep this window open since you will need it later in the assignment.C:\Documents and Settings\wayne>cd c:\ C:\>mkdir introcs C:\>cd introcs C:\introcs>mkdir hello C:\introcs>cd hello C:\introcs\hello>
Click OK and check Save properties for future windows with same title to make the changes permanent.
| Text Editor |
You will type and edit your programs in a text editor called JEdit. JEdit is similar to conventional word processors like MS Word and Notepad, but it features many specialized programming tools including syntax highlighting, bracket matching, auto indenting, indent shifting, line numbering, and commenting out code. It's even written in Java.
| Create the Program |
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 |
It is now time to convert your Java program into a form more amenable for executing on a computer.
If javac complains in some way, you mistyped something, and you should check your program carefully. Ask for help if you can't see the mistake.C:\introcs\hello\>javac HelloWorld.java
Silence is golden in computer science.C:\introcs\hello\>javac HelloWorld.java C:\introcs\hello\>
| Execute the Program |
Now it is time to run your program. This is the fun part.
C:\introcs\hello\>java HelloWorld
C:\introcs\hello\>java HelloWorld Hello, World
| Troubleshooting |
Here are a few suggestions that might help correct any installation woes you are experiencing. If none of these options work (or you don't understand how to follow them), email a staff member.
I can't install JEdit. When I double-click the .jar file, it's not associated with any programs. What can I do? First, make sure that Java 1.4.2 is installed. Then, when it prompts you for an application to assoicate to .jar files, select "Java". Alternatively, from the command prompt, cd to the directory containing .jar file and execute the program as follows:
cd C:\Downloads java -jar jedit41install.jar
When I type, "java -version" I get an error. Check that you edited your Path environment variable as indicated. Close and re-open a command prompt. Type path at the command prompt and look for an entry that includes C:\j2sdk1.4.2_03\bin;. Check that the version number 1.4.2_03 matches the one you installed since Sun updates Java periodicially 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. Any thoughts? It's probably a path issue. Try the suggestions from the previous question.
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.java is in the current directory. The, check your environment variables (system or user) for an entry labeled Classpath. Delete it, close your command prompt, re-open it, and try to execute your program.
Can I use Windows 98 (or ME)? Yes, but it's a rather unstable operating system. You need to modify a few of the instructions. To get a command prompt, type command instead of cmd. To modify your path, edit the file C:\AUTOEXEC.BAT, perhaps under the supervision of a lab TA. You will need to reboot before the changes take effect.