Chapter 3

Working with TextPad

IN THIS CHAPTER

check Acquiring TextPad

check Using TextPad to edit source files

check Compiling Java programs

check 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.

Downloading and Installing TextPad

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:

  1. Choose Configure ⇒ Preferences to open the Preferences dialog box.
  2. Click Tools in the tree that appears on the left side of the dialog box.
  3. 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.

  4. Click OK.

    The commands you need to compile and run Java programs are added to TextPad’s Tools menu.

image

FIGURE 3-1: Configuring tools in TextPad.

Editing Source Files

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.

image

FIGURE 3-2: Editing a Java file in TextPad.

tip When you first create a file (by clicking the New button on the toolbar or by choosing File ⇒ New), TextPad treats the file as a normal text file, not as a Java program file. After you save the file (by clicking the Save button or choosing File ⇒ Save) and assign .java as the file extension, TextPad’s Java-editing features kick in.

The following paragraphs describe some of TextPad’s most noteworthy features for working with Java files:

  • You can’t really tell from Figure 3-2, but TextPad uses different colors to indicate the function of each word or symbol in the program. Brackets are red so that you can spot them quickly and make sure that they’re paired correctly. Keywords are blue. Comments and string literals are green. Other text, such as variable and method names, is black.
  • TextPad automatically indents whenever you type an opening bracket and then reverts to the previous indent when you type a closing bracket. This feature makes keeping your code lined up easy.
  • Line numbers display down the left edge of the editing window. You can turn these line numbers on or off by choosing View ⇒ Line Numbers.
  • To go to a particular line, press Ctrl+G to bring up the Go To dialog box. Make sure that Line is selected in the Go to What box, enter the line number in the text box, and click OK.
  • If you have more than one file open, you can switch between the files by using the Document Selector — the pane on the left side of the TextPad window (refer to Figure 3-2). If the Document Selector isn’t visible, choose View ⇒ Document Selector to summon it.
  • Another way to switch between two (or more) files is to choose View ⇒ Document Tabs. Tabs appear at the bottom of the document window, and you can click these tabs to switch documents.
  • A handy Match Bracket feature lets you pair brackets, braces, and parentheses. To use this feature, move the insertion point to a bracket, brace, or parenthesis and then press Ctrl+M. TextPad finds the matching element.
  • To search for text, press F5. In the Find dialog box, enter the text you’re looking for, and click OK. To repeat the search, press Ctrl+F.
  • To replace text, press F8.

Compiling a Program

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.

image

FIGURE 3-3: Error messages displayed by the Java compiler.

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

tip If you double-click the first line of each error message, TextPad takes you to the spot where the error occurred. If you double-click the line with the unclosed string literal message, for example, you’re taken to line 5, and the insertion point is positioned at the spot where the compiler found the error. Then you can correct the error and recompile the program.

remember Often, a single error can cause more than one error message to display, as is the case in Figure 3-3. The error is that I left off a closing quotation mark after the word Hello in line 5. That one error caused all eight error messages!

Running a Java Program

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.

image

FIGURE 3-4: Running a program.

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.

technicalstuff In case you’re wondering, TextPad actually runs your program by creating and running a batch file — a short text file that contains the commands necessary to run your program. This batch file is given a cryptic name, such as tp02a11c.BAT. Here’s the batch file generated for the HelloApp program:

@ECHO OFF
C:
CD "UsersDougDocumentsJava 5EApplicationsBook 1Chapter 3"
"C:Program FilesJavajdk-9injava.exe" HelloApp
PAUSE

Here’s a closer look at these commands:

  • The first command tells MS-DOS not to display the commands in the command window as the batch file executes.
  • The next two commands switch to the drive and directory that contain the java program.
  • Next, the java.exe program is called to run the HelloApp class.
  • Finally, a PAUSE command executes. That command is what displays the Press any key to continue message when the program finishes.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset