public class Main {
    public static void main(String[] args) {
        System.out.println("Welcome to Online IDE!");
        System.out.println("Start coding in Java...");
    }
}

Press Run or Ctrl ↵ to execute Main.java.
Type program input here while it runs.

Java

Online Java Compiler

Code compiles with javac from OpenJDK 21 (Eclipse Temurin builds) and runs with java; run settings switch to Java 8, 11, 17 or 25. Scanner and BufferedReader read what you type in the console while the program runs.

Try it above

How Java runs here

Versions
Java 8, 11, 17, 21 (default), 25 (OpenJDK, Eclipse Temurin)
Build
javac Main.java
Run
java -Xmx384m Main
Limits per run
512 MB memory, 15 s of CPU time on 2 cores, 2 MB of output
Time
Up to 10 minutes a run (30 on a paid plan); waits 2 minutes for input (5 on a paid plan)
Sandbox
Each run gets its own Linux sandbox with no network access. Text files your program writes next to your code open as new tabs.
Good to know
The heap is capped at 384 MB.

A program that reads input

This reads a count, that many numbers and then a full name, and prints them sorted with the sum and average:

Java
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("How many numbers? ");
        int n = in.nextInt();
        int[] a = new int[n];
        System.out.print("Enter " + n + " numbers: ");
        for (int i = 0; i < n; i++) a[i] = in.nextInt();
        in.nextLine(); // skip the rest of that line before reading text
        System.out.print("Your name: ");
        String name = in.nextLine();

        Arrays.sort(a);
        System.out.println("Sorted: " + Arrays.toString(a));
        System.out.printf("Sum %d, average %.2f%n", Arrays.stream(a).sum(), Arrays.stream(a).average().orElse(0));
        System.out.println("Thanks, " + name + "!");
    }
}
Terminal
$ java -Xmx384m Main
How many numbers? 4
Enter 4 numbers: 7 2 9 4
Your name: Ada Lovelace
Sorted: [2, 4, 7, 9]
Sum 22, average 5.50
Thanks, Ada Lovelace!

The numbers can go on one line or on four: nextInt() skips spaces and line breaks alike. The in.nextLine() after the loop is there on purpose, as the next section explains.

Scanner, BufferedReader and the nextLine() trap

nextInt() reads the number and stops before the Enter that followed it. A nextLine() straight after it reads only that leftover line break and returns an empty string. Remove the in.nextLine() line from the program above and the name is never asked for:

Terminal
$ java -Xmx384m Main
How many numbers? 4
Enter 4 numbers: 7 2 9 4
Your name: Sorted: [2, 4, 7, 9]
Sum 22, average 5.50
Thanks, !
  • Fix it the way the sample does: call in.nextLine() once after the last nextInt() to finish that line. Or read every line with nextLine() and convert it with Integer.parseInt(line.trim()).
  • Scanner parses with regular expressions, which is slow once input runs to a few hundred thousand numbers. For judge-sized input use BufferedReader with StringTokenizer, and write the answer through a PrintWriter.
  • A PrintWriter holds output in memory until flush() or close(). Forget both and the program ends having printed nothing.
  • The console can’t send end of input, so while (in.hasNextInt()) keeps waiting after the last number. Read a count first, or type a word such as end, which makes hasNextInt() return false.

The fast version, reading a count and then that many numbers on one line:

Java
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine().trim());
        StringTokenizer st = new StringTokenizer(br.readLine());
        long sum = 0;
        for (int i = 0; i < n; i++) sum += Long.parseLong(st.nextToken());
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
        out.println(sum);
        out.flush(); // without this line nothing is printed
    }
}

Several classes and files

  • Run compiles the open file with javac and starts the class named after it, so Main.java runs as java Main. Set Entry file in run settings to keep running Main.java while you edit another tab.
  • javac also compiles the classes Main uses from other .java files beside it: put a class MathUtil in MathUtil.java, call MathUtil.gcd(84, 36) from Main, and both compile.
  • Press Run with MathUtil.java open and Java looks for main() in MathUtil: Error: Main method not found in class MathUtil, please define the main method as: public static void main(String[] args).
  • Main.java can also hold more classes, records and interfaces, as long as only one top-level class is public and that one is named Main.
  • A package needs a folder (package shop goes in shop/Cart.java), and the playground has none. Projects have folders; the Java IDE page shows one.

What isn’t available

  • Maven and Gradle dependencies. Only the JDK’s own classes are there, so import com.google.gson.Gson stops with error: package com.google.gson does not exist, and the same goes for JUnit, Lombok and Apache Commons.
  • JavaFX. It left the JDK after Java 8, so import javafx.application.Application; fails with error: package javafx.application does not exist. Swing and AWT compile, but a run has no screen to open a window on.
  • Network access. HttpClient and URL connections fail because runs are offline.
  • More than 384 MB of heap. new int[100_000_000] needs 400 MB and ends with java.lang.OutOfMemoryError: Java heap space.
  • assert checks. The run command doesn’t pass -ea, so assert statements are skipped; throw an exception when a check must stop the program.

Errors you’re likely to see

  • “error: class Hello is public, should be declared in a file named Hello.java”: the public class and the file must share a name. Rename the class to Main, or rename the file to Hello.java and run it from that tab.
  • “error: cannot find symbol”, pointing at Scanner: the import is missing. Add import java.util.Scanner; at the top.
  • “error: incompatible types: possible lossy conversion from double to int”: a double is stored in an int, as in int avg = sum / 2.0;. Cast it with (int) or make the variable a double.
  • “error: unreported exception IOException; must be caught or declared to be thrown”: BufferedReader.readLine() can throw. Add throws IOException to main().
  • “Exception in thread "main" java.util.InputMismatchException”: nextInt() met something that isn’t a whole number, such as 3.5 or a name.
  • “java.lang.NumberFormatException: For input string: "12 "”: Integer.parseInt() was handed a space along with the digits. Call trim() first.

javac points at the line as Main.java:4: before each message. After a failed run, Fix with AI explains the error and proposes a change you review before it applies.

Java versions and contest tips

Java 21, the default, has records, sealed interfaces and pattern matching in switch, all used here:

Java
public class Main {
    sealed interface Shape permits Circle, Rect {}
    record Circle(double r) implements Shape {}
    record Rect(double w, double h) implements Shape {}

    static double area(Shape s) {
        return switch (s) {
            case Circle c -> Math.PI * c.r() * c.r();
            case Rect(double w, double h) -> w * h;
        };
    }

    public static void main(String[] args) {
        var shapes = java.util.List.of(new Circle(1), new Rect(2, 3));
        for (var s : shapes) System.out.printf("%s -> %.2f%n", s, area(s));
    }
}
Terminal
$ java -Xmx384m Main
Circle[r=1.0] -> 3.14
Rect[w=2.0, h=3.0] -> 6.00
  • Pick Java 8 when a course or judge still uses it: it has no var (Java 10), List.of (Java 9), switch arrows (Java 14), text blocks (Java 15) or records (Java 16), and compiling there shows which lines would fail.
  • Java 25 accepts compact source files: a Main.java holding only void main() { IO.println("Hi"); } runs without a class declaration.
  • Deep recursion, such as a DFS over 10^5 nodes, can end in StackOverflowError. Run the work in new Thread(null, task, "main", 1 << 26) to give it a 64 MB stack.
  • Join output in a StringBuilder or a PrintWriter and print it once; a System.out.println() per line is slow over many thousands of lines.

Questions

Does my class have to be called Main?

No. Run compiles the open file and starts the class named after it, so public class Hello in Hello.java runs as java Hello. Main.java is just the file the playground starts with.

Which JDK is it?

OpenJDK from Eclipse Temurin: Java 21 by default, and 8, 11, 17 or 25 from run settings.

How do I give several inputs?

Type each value and press Enter, or put them on one line separated by spaces: nextInt() reads them either way. Pasting several lines sends them all at once.

Why is my nextLine() empty?

It read the line break left behind by an earlier nextInt() or nextDouble(). Call in.nextLine() once to skip it before reading text.

Can I use libraries like Gson or JUnit?

No. There’s no Maven or Gradle, so only the classes that come with the JDK can be imported.

Why does my program print nothing?

If it writes through a PrintWriter or BufferedWriter, the text stays in memory until you call flush() or close(). Add out.flush() at the end of main().