The Java Compiler – Java Compilation Process
The Javavac tool serves as the Java compiler included in the JDK. Since it is all Java, it can run on any platform with a Java runtime system. Read more to learn about the Java compilation process.
Java bytecode converts the Java source code into a compiled class. The final class files have a .class extension, while source files are often designated with a .java extension. Each source code file constitutes a single compilation unit.
Java requires that the file name match the class name and permits only one public class per file. When the class name and filename are different, Java generates a compilation error.
A single file can contain several classes as long as only one of them is public. Steer clear of cramming too many classes into one source file. Packing classes together in a.java file only implies actual relationships.
Example:
package animals.birds;
public class Vehicle extends Car{
}
Compile it with the following:
% javac Vehicle.java
Java requires a filename (with the .java extension) to process it, compared to the Java interpreter, which only accepts a class name as an argument.
The program mentioned above creates the class file Vehicle.class, which is located in the same directory as the source file.
The class file must be in the classpath for most applications, but it’s easier to view in the source directory.
When using Java, you can use the -d option to provide a different directory to store the class files that Java creates.
Depending on whether the class is part of a package, class files are either placed in the specified directory, which serves as the root of the class hierarchy, or in a subdirectory beneath it.
Accordingly, the Java compiler partially replaces the utility’s functionality. Javac recompiles the source and class files after comparing each class’s modification times.
A Java class that has been compiled retains the source file from which it was compiled, and Java can recompile it if needed as long as the source file is accessible.
However, by default, Javac only checks source files explicitly referenced by other source files. This situation implies that an outdated class file can go unnoticed and unrecompiled if only referenced by an updated class file.
Using the -d option, you can make Java walk the entire tree of objects, but the results will significantly lengthen compilation time.
Java Compiler (javac) – Java Compilation Process
The Javac tool generates a bytecode class file by interpreting a Java program’s class and interface declarations. It can also process annotations in Java classes and source files.
There are two methods to provide file names for source code to javac:
Source file names must have a .java extension, class file names must have a .class extension, and both source and class files must have root names that identify the class.
Example:
A class called NavTutorial would be written in a source file called NavTutorial.java and compiled into a bytecode class file called NavTutorial.class.
javac Options
Java Interpreter (java)
The Java Interpreter, which starts with the Java command, is the Java Virtual Machine (JVM) that executes the compiled Bytecode found in .class files generated by javac.
After compiling your Java source code into Bytecode (.class files), you execute it in the following manner:
java ClassName
The Java Virtual Machine (JVM) is crucial for running Java programs. It starts by putting the compiled .class file into memory, ensuring the program is ready to run. Next, the JVM checks the Bytecode for security and correctness to protect against possible mistakes or malicious actions.
The Java Virtual Machine (JVM) either directly interprets the Bytecode after it has been checked or uses just-in-time (JIT) compilation to convert it into machine code. Finally, this machine code runs on the user’s own computer, making the program work smoothly.
Java Class File Structure
When you compile a Java source file (.java) using javac, it produces a class file (.class) containing Bytecode, a platform-independent representation of your code or program for the Java Virtual Machine (JVM).
Role of Bytecode in JVM Execution
Bytecode is platform indepedent code generated by java compiler(javac). Java compiles a.java file into Bytecode, saving it as.class files. The Java Virtual Machine (JVM) runs on any platform, not just one.
javac Vehicle.java -> Vehicle.class
How does Bytecode work in JVM Execution?
The Java source code is compiled to Bytecode. Then, the ClassLoader loads the .class file into memory—bytecode checks for security verification, stack overflows, and invalid opcodes. The JVM interprets the Bytecode into native machine code.