Chapter 3
IN THIS CHAPTER
Acquiring TextPad
Using TextPad to edit source files
Compiling Java programs
Running Java programs
TextPad is an inexpensive ($27) text editor that you can integrate with the Java Development Kit (JDK) to simplify the task of coding, compiling, and running Java programs. It isn’t a true integrated development environment (IDE), as it lacks features such as integrated debugging, code generators, and drag-and-drop tools for creating graphical user interfaces.
TextPad is a popular tool for developing Java programs because of its simplicity and speed. It’s ideal for learning Java because it doesn’t generate any code for you. Writing every line of code yourself may seem like a bother, but the exercise pays off in the long run because you have a better understanding of how Java works.
You can download a free evaluation version of TextPad from Helios Software Systems at www.textpad.com
. You can use the evaluation version free of charge, but if you decide to keep the program, you must pay for it. (Helios accepts credit card payment online.)
If the Java JDK is already installed on your computer when you install TextPad, TextPad automatically configures itself to compile and run Java programs. If you install the JDK after you install TextPad, you need to configure TextPad for Java by following these steps:
Click the Add button to reveal a drop-down list of options and then click Java SDK Commands.
Figure 3-1 shows how the Preferences dialog box appears when the Java tools are installed. As you can see, the Tools item in the tree on the left side of the dialog box includes three Java tools: Compile Java, Run Java Application, and Run Java Applet.
Click OK.
The commands you need to compile and run Java programs are added to TextPad’s Tools menu.
Figure 3-2 shows a Java source file being edited in TextPad. If you’ve worked with a Windows text editor before, you’ll have no trouble mastering the basics of TextPad. I won’t go over such basic procedures as opening and saving files because they’re standard; instead, I describe some TextPad features that are useful for editing Java program files.
The following paragraphs describe some of TextPad’s most noteworthy features for working with Java files:
To compile a Java program in TextPad, choose Tools ⇒ Compile Java or use the keyboard shortcut Ctrl + 1. The javac
command compiles the program, and the compiler output is displayed in the Tool Results pane of the TextPad window. If the program compiles successfully, the message Tool completed successfully
appears in the Tool Results pane. If the compiler finds something wrong with your program, one or more error messages are displayed, as shown in Figure 3-3.
In this example, eight compiler error messages are displayed:
javac 1.8.0-ea
C:UsersDougDocumentsJava 4EManuscriptAppsBook 1Chapter 3HelloApp. java:5: error: unclosed string literal
printHello("World!);
^
C:UsersDougDocumentsJava 4EManuscriptAppsBook 1Chapter 3HelloApp. java:5: error: ';' expected
printHello("World!);
^
C:UsersDougDocumentsJava 4EManuscriptAppsBook 1Chapter 3HelloApp. java:8: error: illegal start of expression
public static void printHello(String greetee)
^
C:UsersDougDocumentsJava 4EManuscriptAppsBook 1Chapter 3HelloApp. java:8: error: ';' expected
public static void printHello(String greetee)
^
C:UsersDougDocumentsJava 4EManuscriptAppsBook 1Chapter 3HelloApp. java:8: error: illegal start of expression
public static void printHello(String greetee)
^
C:UsersDougDocumentsJava 4EManuscriptAppsBook 1Chapter 3HelloApp. java:8: error: ';' expected
public static void printHello(String greetee)
^
C:UsersDougDocumentsJava 4EManuscriptAppsBook 1Chapter 3HelloApp. java:8: error: ';' expected
public static void printHello(String greetee)
^
C:UsersDougDocumentsJava 4EManuscriptAppsBook 1Chapter 3HelloApp. java:12: error: reached end of file while parsing
}
^
8 errors
Tool completed with exit code 1
After you compile a Java program with no errors, you can run it by choosing Tools ⇒ Run Java Application or pressing Ctrl+2. A command window opens, in which the program runs. Figure 3-4 shows the HelloApp
program running in a separate window atop the TextPad window.
When the program finishes, the message Press any key to continue
appears in the command window. When you press a key, the window closes, and TextPad comes back to life.
@ECHO OFF
C:
CD "UsersDougDocumentsJava 5EApplicationsBook 1Chapter 3"
"C:Program FilesJavajdk-9injava.exe" HelloApp
PAUSE
Here’s a closer look at these commands:
java
program.java.exe
program is called to run the HelloApp
class.PAUSE
command executes. That command is what displays the Press any key to continue
message when the program finishes.