Section 1.5. Editing, Compiling, and Running a Java Program


[Page 48]

1.5. Editing, Compiling, and Running a Java Program

In this section we discuss the nuts and bolts of how to compile and run a Java program. Java programs come in two different varieties, applications and applets, and the process differs slightly for each of them. We have already discussed some of the main language features of Java applications and applets, 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.

1.5.1. Java Development Environments

A Java programming environment typically consists of several programs 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 Sun Microsystems, the company that developed Java. It is currently known as the Java2 Platform, Standard Edition 5.0 (J2SE 5.0). Versions of J2SE are available for various platforms, including Unix, Windows, and Macintosh computers. Free downloads are available at Sun's Web site at http://java.sun.com/j2se/. (For more details about the J2SE, see Appendix B.)

In some cases, the individual programs that make up the J2SE are available in a single program-development environment known as an integrated development environment (IDE). Some examples include MetroWerk's Codewarrior, Borland's JBuilder, and Sun's own NetBeans IDE. Each of these provides a complete development package for editing, compiling, and running Java applications and applets on a variety of platforms, including Linux and Windows.

Figure 1.10 illustrates the process involved in creating and running a Java program. The discussion that follows assumes that you are using J2SE 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.

Figure 1.10. Editing, compiling, and running HelloWorld.java.
(This item is displayed on page 49 in the print version)


1.5.2. Editing a Program

Any text editor may be used to edit a program by merely typing the program and making corrections as needed. Popular Unix and Linux editors include vi and emacs. Macintosh editors include SimpleText and BBEdit, and the WinEdit and NotePad editors are available for Windows.

As we have seen, a Java program consists of one or more class definitions. We will follow the convention of placing each class definition in its own file. (Normally in Java a source file should contain only one public class definition.) The files containing these classes' definitions must be named ClassName.java where ClassName is the name of the public Java class contained in the file.

Java Language Rule: File Names

A file that defines a public Java class named ClassName must be saved in a text file named ClassName.java. Otherwise an error will result.



[Page 49]

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

Java Language Rule: Case Sensitivity

Java is case sensitive, which means that it treats helloWorld and Helloworld as different names.


1.5.3. Compiling a Program

Recall that before you can run a Java source program you have to compile it into the Java bytecode, the intermediate code understood by the Java Virtual Machine (JVM). Source code for applets and applications 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.


[Page 50]

J2SE 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, our browser, such as Internet Explorer and Netscape Navigator, must contain a plug-in 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 environmentssuch as within Linux or at the Windows command promptHelloWorld.java would be compiled by typing the following command at the system prompt:

javac HelloWorld.java 


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

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

java HelloWorld 


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 a Macintosh system or within an IDE, neither of which typically has 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 "HelloWorld!" 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 application program will cause the "HelloWorld" message to be displayed on a standard output device of some kind (Fig. 1.11).


[Page 51]

Figure 1.11. Running the HelloWorld.java application program.


1.5.5. Running a Java Applet

To run a Java applet, you need to use either a Web browser or the SDK's appletviewer, a stripped-down Web browser for running applets. The Web browser (or appletviewer) uses an HTML (HyperText Markup Language) document to locate the applet's bytecode files on the Web (or directly on your computer's hard drive). The HTML file must contain an <applet> tag, as shown in Figure 1.12, which tells the browser where to locate the applet's bytecode. (See Appendix B for more details about the appletviewer and the <applet> tag.)

Figure 1.12. An example of an HTML file containing an <applet> tag. This specification will run the Java program named HelloWorldApplet.class.

<html> ... <applet code="HelloWorldApplet.class" width= 200 height=200> </applet> ... </html> 

If the applet tag is correctly specified, the Web browser (or appletviewer) will locate the Java bytecode file on the Web (or on your system's hard drive). It will then load the JVM, which will load the applet's code into memory and interpret and execute it.

The code in Figure 1.12 would be placed in a file named with an .html suffix to indicate that it is an HTML file. The name of the file in this case is not important, but let's suppose we give it the name Hello.html. What is important is to specify the <applet> tag correctly, designating the name of the Java bytecode that is to be executed, for example, HelloWorldApplet.class. It is also necessary for the HTML file to be stored in the same directory or folder as the class file. (There are ways to get around this, but we'll deal with them later.)

Given the correctly coded HTML file, Hello.html, the appletviewer can be used to load and run the applet by typing the following command on the command line:

appletviewer Hello.html 


If you are using a Web browser to run the applet, you would use the browser's menu to load Hello.html into the browser, either across the Internet by supplying its URL (Uniform Resource Locator) or from your local disk by supplying the HTML file's full path name. (You can use the browser's Open File command to located the HTML file.) In any case, the appletviewer or the browser will load the program's bytecode into the computer's main memory and then verify, interpret, and execute the program. The result, as shown in Figure 1.13, is that the "Hello World!" message will be displayed within the browser or appletviewer window.


[Page 52]

Figure 1.13. Running HelloWorldApplet.java applet.





Java, Java, Java(c) Object-Orienting Problem Solving
Java, Java, Java, Object-Oriented Problem Solving (3rd Edition)
ISBN: 0131474340
EAN: 2147483647
Year: 2005
Pages: 275

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net