Java Compiler

Code
Output

        

What Is the Java Online Compiler?

This tool lets you write, compile, and run Java programs directly in your browser without installing anything. It compiles your code on a remote server using the Piston execution engine and returns both compilation and runtime output. Your program must contain a public class named Main with a standard main method. The editor comes pre-loaded with a working example so you can click Run immediately to see how the compiler works. Whether you are learning Java for the first time or quickly testing a snippet, this compiler handles the build process for you and displays results in seconds.

How It Works

  1. Write your Java code in the editor. The entry point must be a public class Main with public static void main(String[] args).
  2. Provide optional input in the STDIN field. This is useful when your program reads from System.in using Scanner or BufferedReader.
  3. Add command-line arguments in the Args field, separated by spaces. These are passed to the args parameter of your main method.
  4. Click Run to compile and execute your program on the remote server.
  5. View the results in the Output panel. If your code has syntax errors, you will see compilation messages. Otherwise, the program's standard output and standard error are displayed.

The entire cycle from submission to output typically completes in one to two seconds, giving you rapid feedback as you iterate on your code.

Step-by-Step Example

Here is a short program that reads a name from standard input, prints a greeting, and lists any command-line arguments:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter your name: ");
        String name = scanner.nextLine();
        System.out.println("Hello, " + name + "!");

        if (args.length > 0) {
            System.out.println("Command-line arguments:");
            for (int i = 0; i < args.length; i++) {
                System.out.println("  " + i + ": " + args[i]);
            }
        } else {
            System.out.println("No command-line arguments provided.");
        }
    }
}

To try this example, paste the code into the editor, type a name such as Alice in the STDIN field, and enter debug verbose in the Args field. Click Run to see the greeting followed by a numbered list of arguments.

Use Cases

Limitations & Notes

Frequently Asked Questions

What Java version is used?

The latest available Java version from the Piston execution engine, typically Java 15 or newer. The exact version may be updated over time as the engine adds support for newer releases.

Why must my class be named Main?

The execution environment looks for a file named Main.java, so your public class must match that filename. This is a requirement of the Java compiler, which enforces that the public class name equals the source file name.

Can I use Scanner for input?

Yes, use Scanner with System.in and enter your input data in the STDIN field. Each line in the STDIN field corresponds to a line your program reads. You can also use BufferedReader wrapped around InputStreamReader if you prefer.

Can I use external libraries like Gson or Apache Commons?

No, only Java standard library classes are available. If your code imports a third-party package, compilation will fail with an error indicating the package does not exist.

Is there a compilation step?

Yes, Java code is compiled first. Compilation errors appear in the output if your code has syntax issues. If compilation succeeds, the compiled program is executed immediately and its output is returned.

Can I create multiple classes?

You can define multiple classes in one file, but only one public class named Main is allowed. Additional classes must omit the public access modifier. This is standard Java behavior for single-file compilation.

Does this support Java modules?

No, module declarations are not supported. Write standard single-file Java programs without module-info.java. The compiler runs in a classpath-based mode.

Is there a time limit?

Yes, execution is limited to prevent infinite loops and long-running processes. If your program exceeds the time limit, it is terminated and a timeout message is displayed in the output panel.

Sources & References

Related Compilers