Page 53 - Introduction to Programming with Java: A Problem Solving Approach
P. 53
Figure 1.8 A command prompt window when it first opens up
Open: box, type cmd (cmd stands for “command”) and click the OK button. That should cause a command prompt window to appear. Figure 1.8 shows the newly opened command prompt window.
In Figure 1.8, note this line:
C:\Documents and Settings\John Dean>
That’s a prompt. In general, a prompt tells you to do something. For a command prompt window, the prompt
tells you to enter a command. Very soon, you’ll enter commands in your actual command prompt window.
Apago PDF Enhancer
Butfirst,notethetextattheleftofthe>symbol.ThetextC:\Documents and Settings\John Dean forms the path to the current directory. A path specifies the location of a directory. More specifically, a path starts with a drive letter and contains a series of one or more slash-separated directory names. In our example,C:referstotheharddrive,Documents and SettingsreferstotheDocuments and Settingsdirectorythat’sontheharddrive,andJohn DeanreferstotheJohn Deandirectorythat’s containedwithintheDocuments and Settingsdirectory.
To compile your Hello World program, you’ll need to go first to the drive and directory where it resides. Suppose your command prompt window’s prompt indicates that your current drive is C:, and you saved Hello.java on F:. Then you’ll need to change your drive to F:. To do so, enter f: in your command prompt window.
To change to the Hello World program’s directory, enter this cd command (cd stands for change directory):
cd \myJavaPgms
Now you’re ready to compile your program. Enter this javac command (javac stands for java compile): javac Hello.java
In entering that command, if your command prompt window displays an error message, refer to Figure 1.9 for possible solutions. If your command prompt window displays no error messages, that indicates success. More specifically, it indicates that the compiler created a bytecode file named Hello.class. To run the Hello.class file, enter this java command:
1.8 First Program—Hello World 19
java Hello