4. Java Program Design and Development

4.6. Editing, Compiling, and Running a Java Pro- gram

In this section we discuss the nuts and bolts of how to compile and run a Java program. Because we are exploring two different varieties of Java programs, console applications and Swing applications, the process dif- fers slightly for each variety. We have already discussed some of the main language features of console and Swing applications, so in this section we focus more on features of the programming environment itself. Because we do not assume any particular programming environment in this book, our discussion will be somewhat generic. However, we do begin with a brief overview of the types of programming environments one might encounter.

 

iting, compiling,

 

lloWorld.java.


User types program into a file

 

using a standard text editor.

Correct the syntax errors

 

 

e source

sk file.

 

javac generates

a list of error messages

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The Java Virtual Machine loads the class file into memory and interprets and runs the bytecode.

 

Java Development Environments

A Java programming environment typically consists of several pro- grams that perform different tasks required to edit, compile, and run a Java program. The following description will be based on the software development environment provided by Oracle, the company that owns and maintains Java. It is currently known as the Java Platform, Standard Edition 8.0 (Java SE 8). Versions of Java SE are available for various platforms, including Linux, Windows, and ma- cOS computers. Free downloads are available at Sun’s Web site at http://www.oracle.com/technetwork/java/. (For more details about the Java SE, see Appendix B.)

In some cases, the individual programs that make up the Java SE are available in a single program development environment, known as an integrated development environment (IDE). Some examples include Eclipse, jGrasp, and Oracle’s own NetBeans IDE. Each of these provides a com- plete development package for editing, compiling, and running Java ap- plications on a variety of platforms, including Linux, macOS, and Win- dows.

Figure 1.10 illustrates the process involved in creating and running a Java program. The discussion that follows here assumes that you are us-

 

ing the Java SE as your development environment to edit, compile and run the example program. If you are using some other environment, you will need to read the documentation provided with the software to determine exactly how to edit, compile, and run Java programs in that environment.

Editing a Program

Any text editor may be used to edit the program by merely typing the program and making corrections as needed. Popular Unix and Linux editors include vim and emacs. These editors are also available on ma- cOS and Windows. However, free macOS editors include TextMate and TextWrangler, and Windows has Notepad++ for free.

As we have seen, a Java program consists of one or more class def-

initions. We will follow the convention of placing each class definition in its own file. (The rule in Java is that a source file may contain only one public class definition.) The files containing these classes’ defini- tions must be named ClassName.java where ClassName is the name of the public Java class contained in the file.

 

For example, in the case of our HelloWorld application program, the file must be named HelloWorld.java, and for HelloWorldSwing, it must be named HelloWorldSwing.java. Because Java is case sensitive, which means that Java pays attention to whether a letter is typed uppercase or lowercase, it would be an error if the file containing the HelloWorld class were named helloworld.java or Helloworld.java. The er- ror in this case would be a semantic error. Java would not be able to find the HelloWorld class because it will be looking for a file named HelloWorld.java.

 

Compiling a Program

Recall that before you can run a Java source program you have to com- pile it into the Java bytecode, the intermediate code understood by the Java Virtual Machine (JVM). Source code for both applets and applica- tions must be compiled. To run a Java program, whether an applet or an application, the JVM is then used to interpret and execute the bytecode.

The Java SE comes in two parts, a runtime program, called the Java Runtime Environment (JRE) and a development package, called the Software Development Kit (SDK). If you are just going to run Java programs, you need only install the JRE on your computer. In order to run Java applets, browsers, such as Internet Explorer and Netscape Navigator, must contain a plugin version of the JRE. On the other hand, if you are going to be developing Java programs, you will need to install the SDK as well.

 

The Java SDK compiler is named javac. In some environments— such as within Linux or at the Windows command prompt HelloWorld.java would be compiled by typing the following com- mand at the system prompt:

,,

 

J

As Figure 1.10 illustrates, if the HelloWorld.java program does not contain errors, the result of this command is the creation of a Java bytecode file named HelloWorld.class—a file that has the same prefix as the source file but with the suffix .class rather than .java. By default, the bytecode file will be placed in the same directory as the source file. If javac detects errors in the Java code, a list of error messages will be printed.

Running a Java Application Program

In order to run (or execute) a program on any computer, the program’s executable code must be loaded into the computer’s main memory. For Java environments, this means that the program’s .class file must be loaded into the computer’s memory, where it is then interpreted by the Java Virtual Machine. To run a Java program on Linux systems or at the Windows command prompt, type

,,

 

 

 

on the command line. This command loads the JVM, which will then load and interpret the application’s bytecode (HelloWorld.class). The “HelloWorld” string will be displayed on the command line.

On Macintosh systems, or within an IDE, which do not typically have a command line interface, you would select the compile and run commands from a menu. Once the code is compiled, the run command will cause the JVM to be loaded and the bytecode to be interpreted. The “Hello, World!” output would appear in a text-based window that automatically pops up on your computer screen. In any case, regardless of the system you use, running the HelloWorld console application program will cause the “Hello, World!” message to be displayed on some kind of standard output device (Fig. 1.11).

Running a Java Swing Program

When you run a Java Swing Program, there is typically no console output. You only see your output in the Window (JFrame) that your Graphics are displayed in. This makes automated testing more difficult since you need to visually inspect that the program is working correctly.

When you run


J

 

 

 

 

 

 

Figure 1.11: Compiling and Run- ning the HelloWorld.java con- sole application program.

 

,,

 

J

A window will open, and you won’t be able to type in the console until you close the window, quit the program, or type ctl-c to send a kill signal to the Swing program. The result of running, as shown in Figure 1.12,

 

is that the “Hello, World!” message will be displayed within it’s own window.

 

 

 

 

 

 

 

 

 

 

 

java.sun.com/j2se/1.5.0/docs/api/