, ,

Compiling a Program – Basics of Java Programming

Compiling a Program

The JDK provides tools for compiling and running programs. The classes in the Java SE Platform API are already compiled, and the JDK tools know where to find them.

Java source files can be compiled using the Java Language Compiler, javac, which is part of the JDK. Each source file name has the extension .java. Each class declaration in a source file is compiled into a separate class file, containing its Java bytecode. The name of this file comprises the name of the class with .class as its extension.

The source files Point2D.java, Point3D.java, and TestPoint3D.java contain the declarations of the Point2D, Point3D, and TestPoint3D classes, respectively. The respective source files are in the same directory. The source files can be compiled by giving the following javac command on the command line (the character > is the command prompt and we will use bold type for anything typed on the command line):

Click here to view code image

>javac Point2D.java Point3D.java TestPoint3D.java

This javac command creates the class files Point2D.class, Point3D.class, and TestPoint3D.class containing the Java bytecode for the Point2D, Point3D, and TestPoint3D classes, respectively. The command creates the class files in the same directory as the source files.

Although a Java source file can contain more than one class declaration, the Java compiler enforces the rule that there can only be at the most one class in the source file that has public access. If there is a public class in the source file, the name of the source file must be the name of the public class with .java as its extension. In the absence of a public class in the source file, the name of the file can be arbitrary, but still with the .java extension. Regardless, each class declaration in a source file is compiled into a separate .class file.

Related Posts