The Wayback Machine - https://web.archive.org/web/20261015134629/https://www.geeksforgeeks.org/java/compilation-execution-java-program/

Compilation and Execution of a Java Program

Last Updated : 14 Oct, 2025

Java is a high-level, platform-independent language. To run a Java program, it must go through compilation and execution. Understanding this process is essential for beginners. For example, a simple Java program:

Java
public class Geeks{
    
    public static void main(String[] args){
        
        System.out.println("Hello, Geeks");
    }
}

Output
Hello, Geeks

How to run the above code?

  • Write code in a file like Geeks.java.
  • The Java Compiler "javac" compiles it into bytecode "Geeks.class".
  • The JVM (Java Virtual Machine) reads the .class file and interprets the bytecode.
  • JVM converts bytecode to machine readable code i.e. "binary" (001001010) and then execute the program.
geeks

Steps to Compile and Execute a Java Program

Running a Java program involves two main steps: compilation and execution. Let’s look at each step in detail.

Step 1: Writing the Java Program

Before you can run a Java program, you need to write it in a text editor. You can use any text editor like Notepad (Windows), TextEdit (Mac), or an IDE (Integrated Development Environment) like Eclipse, IntelliJ IDEA, or VS Code.

Example:

  • Class name: Geeks
  • File name: Geeks.java
out1
output

Note: Save the file with a .java extension and make sure the filename matches the class name.

Step 2: Compiling the Java Program

Once the Java program is written, it needs to be compiled. Compilation is the process of converting the human-readable Java code into bytecode, which can be understood by the Java Virtual Machine (JVM).

Command to compile:

javac Geeks.java

Here:

  • javac is the Java compiler.
  • Geeks.java is the file to compile.
out1
Output

If there are no errors, this command will create a file called Geeks.class. The .class file contains bytecode, which is a platform-independent code that the JVM can execute. If there are errors, the compiler will show them in the terminal. You need to fix the errors before running the program.

Step 3: Executing the Java Program

After successful compilation, you can run the program using the Java interpreter.

Command to run:

java Geeks

out1
output

Here:

  • java is the Java interpreter.
  • Geeks is the name of the class containing the main method (do not include .class).

When executed, you will see:

Hello, Geeks

Comment