Lesson 1.9: Compilation and Execution Process
Introduction
Have you ever wondered what happens after you click the Run button in a Java program?
Does the computer understand Java code directly?
The answer is No.
A computer understands only machine language (binary code), but Java programs are written in human-readable source code. Therefore, Java follows a special process to convert your code into machine language.
In this lesson, you will learn how a Java program is compiled and executed step by step.
Learning Objectives
After completing this lesson, you will be able to:
-
Understand the Java compilation process.
-
Explain how Java programs are executed.
-
Understand Source Code, Bytecode, and Machine Code.
-
Explain the role of the Java Compiler and JVM.
-
Understand why Java is platform-independent.
Java Program Execution Process
The execution of a Java program consists of five main steps:
-
Write the Source Code
-
Compile the Program
-
Generate Bytecode
-
Execute Bytecode using the JVM
-
Display the Output
Step 1: Write the Source Code
A Java program is written in a text file with the .java extension.
Example:
public class Hello { public static void main(String[] args) { System.out.println("Hello Java"); } }
File Name:
Hello.java
This file is called the Source Code.
Step 2: Compile the Program
The Java Compiler (javac) converts the source code into bytecode.
Command:
javac Hello.java
If there are no errors, the compiler creates a new file:
Hello.class
This file contains Java Bytecode.
What is Bytecode?
Bytecode is an intermediate code generated by the Java compiler.
It is not machine language.
Instead, it is a special code that can be understood by the Java Virtual Machine (JVM).
Because of bytecode, Java programs can run on different operating systems without changing the source code.
Step 3: Execute the Bytecode
Run the following command:
java Hello
The JVM loads the Hello.class file.
It then converts the bytecode into machine code that the computer’s processor can execute.
Finally, the program runs and displays the output.
Output
Hello Java
Java Compilation and Execution Flow
The complete process can be represented as follows:
Hello.java
│
▼
Java Compiler (javac)
│
▼
Hello.class (Bytecode)
│
▼
Java Virtual Machine (JVM)
│
▼
Machine Code
│
▼
Output on Screen
Role of the Java Compiler
The Java Compiler (javac) performs several important tasks:
-
Checks syntax errors.
-
Converts source code into bytecode.
-
Creates the
.classfile. -
Reports compilation errors.
If your program contains errors, the compiler stops the process and displays error messages.
Role of the JVM
The Java Virtual Machine (JVM) is responsible for:
-
Loading bytecode.
-
Verifying bytecode.
-
Converting bytecode into machine code.
-
Executing the program.
-
Managing memory.
-
Performing Garbage Collection.
-
Handling exceptions.
Without the JVM, Java programs cannot run.
Why is Java Platform Independent?
Different operating systems understand different machine languages.
Instead of generating machine code directly, Java generates bytecode.
Each operating system has its own JVM.
The JVM converts the same bytecode into machine code suitable for that operating system.
This is why Java follows the principle:
Write Once, Run Anywhere (WORA).
Common Compilation Errors
1. Missing Semicolon
Incorrect:
System.out.println("Hello")
Correct:
System.out.println("Hello");
2. File Name and Class Name Do Not Match
Incorrect:
File:
Demo.java
Class:
public class Hello
Correct:
File:
Hello.java
Class:
public class Hello
3. Missing Curly Braces
Every opening brace { must have a matching closing brace }.
4. Incorrect Capitalization
Incorrect:
system.out.println("Hello");
Correct:
System.out.println("Hello");
Java is case-sensitive.
Difference Between Compilation and Execution
| Compilation | Execution |
|---|---|
| Converts source code into bytecode | Runs the bytecode |
Performed by the Java Compiler (javac) |
Performed by the JVM |
Produces a .class file |
Displays the program output |
| Reports syntax errors | Reports runtime errors |
Best Practices
-
Save the file before compiling.
-
Ensure the file name matches the public class name.
-
Read compiler error messages carefully.
-
Correct all compilation errors before running the program.
-
Keep your code properly formatted.
Key Points to Remember
-
Java source files have the
.javaextension. -
The Java Compiler converts source code into bytecode.
-
Bytecode is stored in a
.classfile. -
The JVM executes the bytecode.
-
Java programs are platform-independent because of bytecode and the JVM.
Lesson Summary
In this lesson, you learned:
-
The complete Java compilation and execution process.
-
The roles of the Java Compiler and JVM.
-
The meaning of source code, bytecode, and machine code.
-
Why Java is platform-independent.
-
Common compilation errors and how to fix them.
Understanding this process will help you troubleshoot Java programs and understand how Java works behind the scenes.
Practice Questions
-
What is source code?
-
What is bytecode?
-
Which command compiles a Java program?
-
Which command executes a Java program?
-
What is the role of the Java Compiler?
-
What is the role of the JVM?
-
Why is Java platform-independent?
-
What file is created after successful compilation?
Programming Exercise
-
Create a file named
Test.java. -
Write a program that prints “Java Programming is Fun!”.
-
Compile the program using the command prompt.
-
Run the program.
-
Observe the generated
.classfile.
Assignment
Perform the following tasks:
-
Write a Java program that displays your name and city.
-
Compile the program using
javac. -
Execute it using
java. -
Identify the generated
.classfile. -
Write a short explanation (100–150 words) describing the compilation and execution process in your own words.