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. (Other flavors of Windows are similar, but 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.

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! To continue, return to to the Introduction to Computer Science booksite.

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 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.5 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. 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_06\bin;. Check that the version number 1.5.0_06 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_06\bin exists.

How can I check the values of my PATH and CLASSPATH variables? Type the following at the command prompt.

C:\introcs\hello\> echo %PATH%
C:\introcs\hello\> echo %CLASSPATH%
The PATH variable should begin with C:\Program Files\Java\jdk1.5.0_06\bin;. The CLASSPATH variable should either be empty (%CLASSPATH%) or it should begin with .;.

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 have an environment variable (system or user) set for the CLASSPATH. If so, prepend .; to the beginning of your CLASSPATH variable using the same method that you used to edit your PATH variable. (iTunes has a proclivity for changing the CLASSPATH variable, so if you recently upgraded iTunes, this is likely the source of the problem.)

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.