< Day Day Up > |
Creating a Sample ProgramNow 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 HelloUser.java. Listing A.1. Source Code of HelloUser.java1: public class HelloUser { 2: public static void main(String[] arguments) { 3: String username = System.getProperty("user.name"); 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 book, you can compare it to a copy on the book's official World Wide Web site at the following address: http://www.java24hours.com After you finish typing in the program, save the file somewhere on your hard drive with the name HelloUser.java. Java source files must be saved with the extension .java.
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, HelloUser.java is saved as HelloUser.java.txt. As a work-around to avoid this problem, place quotation marks around the filename when saving a source file. Figure A.9 shows this technique being used to save the source file HelloUser.java from Windows Notepad. Figure A.9. Saving a source file from Windows Notepad.
The purpose of this project is to test the Java Development Kit—none of the Java programming 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 WindowsNow 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 HelloUser.java. 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 HelloUser.java by entering the following at a command prompt:\ javac HelloUser.java Figure A.10 shows the MS-DOS commands used to switch to the \J24work folder and compile HelloUser.java. Figure A.10. Compiling a Java program in a command-line window.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 HelloUser.java. 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. Figure A.11 shows the successful output of the HelloUser application along with the commands used to get to that point. Figure A.11. Running a Java application.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 book. If you cannot get the program to compile successfully even though you have typed it exactly at it appears in the book, 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. |
< Day Day Up > |