Creating a Sample Program

Now that you have installed and set up the Java Development Kit, you're ready to create a sample Java program to make sure that it works. Java programs begin as source code—a series of statements created using a text editor and saved as a text file. You can use any program you like to create these files, as long as it can save the file as plain, unformatted text. The Java Development Kit does not include a text editor, but most other Java development tools include a built-in editor for creating source code files. Run your editor of choice and enter the Java program in Listing A.1. Make sure all the parentheses, braces, and quotation marks in the listing are entered correctly and capitalize everything in the program exactly as shown. If your editor requires a filename before you start entering anything, call it

Listing A.1. Source Code of
1: public class HelloUser {
2: public static void main(String[] arguments) {
3: String username = System.getProperty("");
4: System.out.println("Hello " + username);
5: }

The line numbers and colons along the left side of Listing A.1 are not part of the program—they're included, so that I can refer to specific lines by number in each program. If you're ever unsure about the source code of a program in this tutorial, you can compare it to a copy on the tutorial's official World Wide Web site at the following address:

After you finish typing in the program, save the file somewhere on your hard drive with the name Java source files must be saved with the extension .java.

Did you Know?

If you have created a folder called J24work, save and all other Java source files from this tutorial in that folder. This makes it easier to find them while using a command-line window.

If you're using Windows, a text editor such as Notepad might add an extra .txt file extension to the filename of any Java source files you save. For example, is saved as As a work-around to avoid this problem, place quotation marks around the filename when saving a source file. Screenshot A.9 shows this technique being used to save the source file from Windows Notepad.

Screenshot A.9. Saving a source file from Windows Notepad.

[View full size image]Java ScreenShot

Did you Know?

A better solution is to permanently associate .java files with the text editor you'll be using: In Windows, open the folder that contains and double-click the file. If you have never opened a file with the .java extension, you'll be asked what program to use when opening files of this type. Choose your preferred editor and select the option to make your choice permanent. From this point on, you can open a source file for editing by double-clicking the file.

The purpose of this project is to test the Java Development Kit—none of the Java coding concepts used in the five-line HelloUser program are described in this appendix. You'll learn the basics of the language during the four hours of Part 1, "Getting Started!" If you have figured out anything about Java simply by typing in the source code of Listing A.1, it's entirely your own fault.

Compiling and Running the Program in Windows

Now you're ready to compile the source file with the Java Development Kit's Java compiler, a program called javac. The compiler reads a .java source file and creates one or more .class files that can be run by a Java interpreter. Open a command-line window, and then open the folder where you saved If you saved the file in the J24work folder inside the root folder on your main hard drive, the following MS-DOS command opens the folder:

cd \J24work

When you are in the correct folder, you can compile by entering the following at a command prompt:\


Screenshot A.10 shows the MS-DOS commands used to switch to the \J24work folder and compile

Screenshot A.10. Compiling a Java program in a command-line window.

[View full size image]Java ScreenShot

The Java Development Kit compiler does not display any message if the program compiles successfully. If there are problems, the compiler enables you to know by displaying each error along with a line that triggered the error. If the program compiled without any errors, a file called HelloUser.class is created in the same folder that contains The class file contains the Java bytecode that will be executed by a Java interpreter. If you get any errors, go back to your original source file and make sure that you typed it exactly as it appears in Listing A.1. After you have a class file, you can run that file using a Java interpreter. The JDK's interpreter is called java, and it also is run from the command line. Run the HelloUser program by switching to the folder containing HelloUser.class and entering the following:

java HelloUser

You will see the text "Hello" followed by a comma and your username.

By the Way

When running a Java class with the kit's Java interpreter, don't specify the .class file extension after the name of the class. If you do, you'll see an error such as the following:

Exception in thread "main" java.lang.NoClassDefFoundError:
Java ScreenShot HelloUser/class

Screenshot A.11 shows the successful output of the HelloUser app along with the commands used to get to that point.

Screenshot A.11. Running a Java app.

[View full size image]Java ScreenShot

If you can compile the program and run it successfully, your Java Development Kit is working and you are ready to start Hour 1 of this tutorial. If you cannot get the program to compile successfully even though you have typed it exactly at it appears in the tutorial, there may be one last problem with how the Java Development Kit is configured on your system: the CLASSPATH environment variable might need to be configured.