GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • JVM
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Compilation and Execution of a Java Program - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

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

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.