1.10. Anatomy of a Java Program

 
[Page 18 ( continued )]

1.9. Creating, Compiling, and Executing a Java Program

You have to create your program and compile it before it can be executed. This process is repetitive, as shown in Figure 1.11. If your program has compilation errors, you have to fix them by modifying the program, then recompile it. If your program has runtime errors or does not produce the correct result, you have to modify the program, recompile it, and execute it again.

Figure 1.11. The Java programming-development process consists of creating/modifying source code, compiling, and executing programs.

You can use any text editor to create and edit a Java source code file, or you can use an IDE like Eclipse, JBuilder or NetBeans. Figure 1.12 shows how to use NotePad to create and edit the source code file.


[Page 19]
Figure 1.12. You can create the Java source file using Windows NotePad.


This file must end with the extension .java and must have the exact same name as the public class name. For example, the file for the source code in Listing 1.1 should be named Welcome.java, since the public class name is Welcome .

A Java compiler translates a Java source file into a Java bytecode file. The following command compiles Welcome.java:

  javac Welcome.java  

Note

You must first install and configure JDK before compiling and running programs. See Supplement I.B, "Installing and Configuring JDK 5.0," on how to install JDK and how to set up the environment to compile and run Java programs. If you have trouble compiling and running programs, please see Supplement I.C, "Compiling and Running Java from the Command Window." This supplement also explains how to use basic DOS commands and how to use Windows NotePad and WordPad to create and edit files. All the supplements are accessible from the Companion Website.


Caution

Java source programs are case-sensitive . It would be wrong, for example, to replace main in the program with Main . Program filenames are case-sensitive on UNIX but generally not on Windows; JDK treats filenames as case-sensitive on any platform. If you try to compile the program using javac welcome.java , you will get a file-not-found error.


If there are no syntax errors, the compiler generates a bytecode file with a .class extension. So the preceding command generates a file named Welcome.class . The bytecode is similar to machine instructions but is architecture-neutral and can run on any platform that has a JVM. This is one of Java's primary advantages: Java bytecode can run on a variety of hardware platforms and operating systems.

To execute a Java program is to run the program's bytecode. You can execute the bytecode on any platform with a JVM. The following command runs the bytecode:

  java Welcome  

Figure 1.13 shows the javac command for compiling Welcome.java. The compiler generated the Welcome .class file . This file is executed using the java command.

Figure 1.13. The output of Listing 1.1 displays the message Welcome to Java!


[Page 20]

Caution

Do not use the extension .class in the command line when executing the program. The JVM assumes that the first argument in the command is the filename and then fetches filename.class to execute. It would attempt to fetch filename.class.class if you used java filename.class in the command line.


Tip

If you execute a class file that does not exist, a NoClassDefFoundError exception will occur. If you execute a class file that does not have a main method or you mistype the main method (e.g., by typing Main instead of main ), a NoSuchMethodError will occur.


Note

When executing a Java program, the JVM first loads the bytecode of the class to memory using a program called the class loader . If your program uses other classes, the class loader dynamically loads them just before they are needed. After a class is loaded, the JVM uses a program called bytecode verifier to check the validity of the bytecode and ensure that the bytecode does not violate Java's security restrictions. Java enforces strict security to make sure that Java programs arriving from the network do not harm your computer.


 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

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