☕ Java OpenJDK Runtime

Free Online Java Compiler

Write, compile, and execute Java programs online instantly with OpenJDK runtime, Monaco IDE editor, and interactive input execution.

Overview of Java Compiler

Java is an object-oriented, class-based, concurrent programming language built around the principle of "Write Once, Run Anywhere" (WORA). Used by millions of enterprise developers and taught in computer science curricula globally, Java underpins backend web APIs, Android applications, and large-scale distributed systems. Our Online Java Compiler lets you write, compile, and execute Java programs online without configuring JDK path variables (JAVA_HOME) or installing heavy desktop IDEs like IntelliJ IDEA or Eclipse. Simply type your class code, click Run, and view stdout and stderr output in seconds.

How to Use the Java Compiler

  1. Write your Java class inside the editor window.
  2. Ensure your main method (public static void main(String[] args)) is included.
  3. Provide required inputs in the Stdin terminal if using Scanner or BufferedReader.
  4. Click the green ▶ Run Code button to trigger javac compilation and execution.
  5. Review stdout results, execution speed, and return codes in the terminal output.
  6. Use multi-tab program feature to organize different Java exercise files.

🧪 Tested Code Examples (Java)

Example 1: Hello World & Class Structure

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World from Online Java Compiler!");
        
        int a = 10, b = 20;
        System.out.println("Sum: " + (a + b));
    }
}
Expected Output:
Hello, World from Online Java Compiler!
Sum: 30

Code Explanation: In Java, every executable program must reside inside a class with a main method matching public static void main(String[] args).

Example 2: Scanner Input & Control Flow

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter number: ");
        int num = scanner.nextInt();
        
        if (num % 2 == 0) {
            System.out.println(num + " is Even");
        } else {
            System.out.println(num + " is Odd");
        }
    }
}
Expected Output:
Enter number: 14
14 is Even

Code Explanation: java.util.Scanner reads primitive types from System.in. The modulus operator (%) checks for even divisibility by 2.

Example 3: Object-Oriented Class & Methods

class Rectangle {
    int width, height;
    
    Rectangle(int w, int h) {
        this.width = w;
        this.height = h;
    }
    
    int getArea() {
        return width * height;
    }
}

public class Main {
    public static void main(String[] args) {
        Rectangle rect = new Rectangle(5, 8);
        System.out.println("Rectangle Area: " + rect.getArea());
    }
}
Expected Output:
Rectangle Area: 40

Code Explanation: Demonstrates Java Object-Oriented principles including class instantiation, constructor initialization, and method invocation.

💡 Try modifying these examples and execute them directly in the compiler editor above!

🛠️ Common Java Compiler Errors & Fixes

⚠️ cannot find symbol

Cause: Typo in variable name, missing import (e.g. java.util.Scanner), or out-of-scope variable usage.

How to Fix: Verify variable declaration spelling and add appropriate import statements at top of file.

⚠️ class Main is public, should be declared in a file named Main.java

Cause: Public class name does not match source file name.

How to Fix: Our auto-preprocessor handles class rename, or ensure main class matches public class name.

⚠️ NullPointerException

Cause: Attempting to call methods or access fields on an uninitialized null object reference.

How to Fix: Instantiate objects with new keyword before accessing member methods.

⚠️ ArrayIndexOutOfBoundsException

Cause: Accessing array index less than 0 or greater than or equal to array length.

How to Fix: Check loop boundaries and array size before accessing elements.

Frequently Asked Questions (FAQ)

Q: Must my Java class be named Main?

By default, our Java runner uses class Main, but our pre-processor automatically detects public class names and adjusts them so your code compiles smoothly.

Q: Does this Java compiler support Object-Oriented Programming (OOP)?

Yes! You can define classes, methods, inheritance, interfaces, polymorphism, encapsulation, and exception handling.

Q: Can I test Java Scanner input online?

Yes! You can interactively provide inputs when java.util.Scanner or BufferedReader reads from System.in.

Q: Is this Java compiler free for classroom use?

Yes, 100% free for students, educators, and coding bootcamp practice.