Hello, World in Java on Windows


This document instructs you on how to setup a Java programming environment for your Windows XP computer and provides a step-by-step guide for 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.


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.

Compile the Program

It is now time to convert your Java program into a form more amenable for execution on a computer.

Execute the Program

Now it is time to run your program. This is the fun part.

Congratulations, you are now a Java programmer!

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 associate 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 jedit42install.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_07\bin;. Check that the version number 1.4.2_07 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. 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.

I followed the instructions above for associating .java files with JEdit, but it doesn't work on my system. How can I fix it? Do the following steps.

If you get a Restore button instead of Advanced, click it and it becomes Advanced.

Can I use Windows 98 or ME? Yes, but we strongly discourage you from doing so since they're rather unstable operating systems. 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.