1. Learning about the Java SE Development Kit

    Several versions of Java are available for free at the Java website (www.oracle.com/ technetwork/java/index.html). The official name of the most recent version is Java Platform, Standard Edition 9, often called Java SE 9 for short. The number 9 reflects Java’s evolving level of maturity. As updates to existing versions emerge or entirely new versions containing advanced features are released, you can download them. The different names for Java configurations are somewhat confusing and frequently misused. If you download Java to use with this book, you want to acquire the Java Standard Edition (SE) Development Kit, also known as the JDK. Java also supports the Java Enterprise Edition (EE), which includes all of the classes in the Java SE, plus a number of classes that are more useful to programs running on servers than on workstations. The Java EE Development Kit is known as SDK. The names of the development kits have changed frequently; originally, JDK meant Java Development Kit, but that interpretation was used with the earliest Java versions and is no longer used officially. The Java Micro Edition (ME) is another Java platform, which is used for devices such as cell phones and other small consumer appliances.
  2. Configuring Windows to Use the JDK

    To configure your Windows operating system with the JDK, you must add the Java bin directory to the command path of your operating system (OS). That way, your OS will know where to look for the Java commands that you use. One way to update the OS path for Windows is to edit or set the OS path in the autoexec.bat file. This file is automatically executed every time you start your computer. A simpler and less error-prone alternative is to type two commands at the command prompt when you want to begin a session of working on Java programs. (These two commands are described later in this appendix.) You do not need to be an operating system expert to issue operating system commands. Learning just a few commands allows you to create and run all the examples in this book.
    Finding the Command Prompt
    To locate the command prompt on your Windows 10 computer, you can click in the Search box at the bottom left of your screen and start to type Command Prompt. When Command Prompt appears in the list, click it. Several alternate ways to access the command prompt are available; using a browser, search for Launch command prompt
    Command Prompt Anatomy
    The Windows command prompt contains at least a disk drive name followed by a colon, a backslash, and a greater-than sign (for example, C:\>). You might also see folder or directory names within the command prompt just before the greater-than sign, as shown in the following examples: C:\Users\YourUserName> C:\Documents and Settings> C:\Documents and Settings\Administrator> Each directory in the path is separated by a backslash.
    Changing Directories
    You can back up one directory level by typing cd for “change directory,” followed by two periods: cd.. For example, if your command prompt contains C:\Users\ and you type cd.., the command prompt changes to C:\Users>. If you type cd.. again, the prompt changes to C:\>, indicating the root directory. When you have multiple directories to back through, it is easier to use the following command: cd\ This takes you immediately to the root directory instead of backing up one level at a time. At the command prompt, you can change the directory by typing cd followed by the name of the directory. For example, if you have a folder named Java and it contains a folder named Chapter01, you can change the command prompt to the Chapter01 folder by backing up to the root directory and typing the following: cd Java cd Chapter01 After these commands, the command prompt reads C:\Java\Chapter01>. When you compile and execute your Java programs, you should start from the command prompt where the files are stored. When your command prompt display is filled with commands, it can look confusing. If you want, you can type cls (for Clear Screen) to remove old commands.
    Setting the class and classpath Variables
    When you start a Java session, you might need to set the class and classpath options. These settings tell the operating system where to find the Java compiler and your classes. If you or someone else has altered your autoexec.bat file to contain these commands, you do not need to type them. Otherwise, every time you want to compile and execute Java programs, you need to type statements similar to the following: path = c:\program files\java\jdk-9\bin set classpath=. After you have typed the class and classpath statements, you can compile and run as many Java programs as you want without typing these commands again. You must type them again if you close the Command Prompt window or restart your computer. The first statement sets the path and allows the OS to recognize the javac command you use when compiling programs. Consider the following example: path = c:\program files\java\jdk-9\bin This example assumes that you are using JDK 9 and that it is stored in the java folder in the program files folder. These are the defaults when you download Java from the Java website; if you installed Java in a different location, you need to alter the command accordingly. The command set classpath=. tells Java to find your compiled classes in the current directory when you execute your applications. There must be no space between classpath and the equal sign, nor between the equal sign and the period. After you set the path correctly, you should be able to use the javac command. If you attempt to compile a Java class and see an error message that javac is not a recognized command, either Java was not properly installed or the path command was incorrect. If classes compile successfully but do not execute, you might have entered the classpath command incorrectly.
    Changing a File’s Name
    When working through the examples in this book, you often will find it convenient to change the name of an existing file—for example, when you want to experiment with altering code without losing the original version, or if you find that when you previously saved a file, you mistyped a filename so that it did not match the class name within the .java file you created. You can take at least three approaches: • Open the existing file using the appropriate software application (for example, Notepad), click File on the menu bar, and then click Save As. Select the folder you want, then type a new filename for the file. Now you have two versions—one with the old name and one with the new. • Open the folder where the file is located and find the misnamed file. Select the file and then click the filename. (Do not double-click the filename unless you want to open the file.) You can then edit the filename by using a combination of the Backspace, Delete, and character keys. Press Enter when the filename is correct. Alternately, you can rightclick the filename and choose Rename from the menu that appears. • At the command prompt, use the rename command. You type rename, a space, the old filename, another space, and the new filename. For example, to change a file named Xyz.java to Abc.java, type the following at the command prompt for the directory containing the existing file: rename Xyz.java Abc.java
  3. Compiling and Executing a Java Program
    At the command prompt, change from the default drive prompt to the drive where your application is stored. Then change the directory (or folder) to the directory that holds your application. To compile an application, you type the javac command to start the Java compiler, then type a space and the complete name of the .java file—for example, First.java. If the application doesn’t compile successfully, the path might not be set correctly to the Java JDK bin directory where the javac.exe file is located. Also, you might have failed to use the same spelling as the Java filename. When you compile a .java file correctly, the Java compiler creates a .class file that has the same filename as the .java file. Thus, a successful compilation of the First.java file creates a file named First.class. To run a Java application, you use the java command and the class name without the .class extension. For example, after an application named First.java is compiled, producing First.class, you execute the program using the following command: java First After the program executes, control is returned to the command prompt. If a program does not end on its own, or you want to end it prematurely, you can press Ctrl+C to return to the command prompt. After you compile a Java program, you can execute it as many times as you want without recompiling. If you change the source code, you must save and compile again before you can see the changed results in an executed application.