, ,

Running a Program – Basics of Java Programming

Running a Program

It is the bytecode in the class files that is executed when a Java program is run—the source code is immaterial in this regard. A Java program is run by the Java Application Launcher, java, which is also part of the JDK. The java command creates an instance of the JVM that executes the bytecode.

The following java command on the command line will launch the program in Example 1.5:

>
java TestPoint3D

p3A: (10,20,30)

Note that only the name of the class that is the entry point of the program is specified, resulting in the execution starting in the main() method of the specified class. This main() method is found in the class file of the TestPoint3D class. The program in Example 1.5 terminates when the execution of the main() method is completed.

Running a Single-File Source-Code Program

Typically, Java source code is first compiled by the javac command to Java byte-code in class files and then the bytecode in the class files is executed by the java command. The compilation step can be omitted if the complete source code of the program is contained in a single source file, meaning that all class declarations that comprise the program are declared in one source file.

In Example 1.5, the program is composed of three source files: Point2D.java, Point3D.java, and TestPoint3D.java, containing the declarations of the Point2D, Point3D, and TestPoint3D classes, respectively. In Example 1.6, the class declarations are now contained in the Demo-App.java file; in other words, the complete source code of the program is now in a single source file. We can run the program with the following java command, without compiling the source code first:

>
java Demo-App.java

p3A: (10,20,30)

The full name of the single source file is specified in the command line. Full program output is shown in Example 1.6.

Note that no class files are created. The source code is compiled fully in memory and executed.

In order to run a single-file source-code program, the following conditions must be met:

  • The single source file must contain all source code for the program.
  • Although there can be several class declarations in the source file, the first class declaration in the source file must provide the main() method; that is, it must be the entry point of the program.
  • There must not exist class files corresponding to the class declarations in the single source file that are accessible by the java command.

Unlike the javac command, the name of the single source file (e.g., Demo-App.java) need not be a valid class name, but it must have the .java extension. Also unlike the javac command, the java command allows several public classes in the single source file (only public classes in the Demo-App.java file).

Examples of single-file source-code programs can be found throughout the book.

Example 1.6 A Single-File Source-Code Program

Click here to view code image

// File: Demo-App.java
public class TestPoint3D {
  // Same as in Example 1.5.
  // Provides the main() method and is the first class declaration in the file.
}

public class Point2D {
  // Same as in Example 1.2.
}

public class Point3D extends Point2D {
  // Same as in Example 1.3.
}

Running the program:

Click here to view code image

>
java Demo-App.java

p3A: (10,20,30)
x: 10
y: 20
z: 30
p3A: (-10,-20,-30)
p3B: (30,20,10)
Distance between p3A and p3B: 69.2820323027551
A 3D point represented by (x,y,z)-coordinates.

Related Posts