Online Java Compiler
Write, run and test Java code online, right in your browser. This free online Java compiler needs no installation or signup — just type your code and click Run.
Java is an object-oriented, statically typed language that runs on the JVM and powers Android apps, enterprise systems and backend services.
How to use the Java compiler
- Write or paste your Java code in the editor.
- If your program reads input, open the I/O tab and type it into the STDIN box (one value per line).
- Click Run — the output and any errors appear in the output panel.
Sample: read input in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
System.out.println("Hello, " + name + "!");
}
}
Paste this into the editor, type your name in the STDIN box and click Run.
More Java examples to try
Read two numbers and add them
Uses Scanner to read two integers from STDIN (for example 12 30).
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println("Sum = " + (a + b));
}
}
Classes and objects
A small class with a constructor and a method — the core of object-oriented Java.
class Student {
String name;
int marks;
Student(String name, int marks) {
this.name = name;
this.marks = marks;
}
String grade() {
return marks >= 80 ? "A" : marks >= 60 ? "B" : "C";
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student("Asha", 86);
System.out.println(s.name + " got grade " + s.grade());
}
}
Common Java errors and how to fix them
error: class X is public, should be declared in a file named X.java- Keep your main class named
Main(the editor file is Main.java), or rename the file to match your public class. java.util.NoSuchElementExceptionScannertried to read input that was not provided. Add the values in the STDIN box before running.error: ';' expected- A statement is missing its semicolon. Check the line mentioned in the error and the line just above it.
java.lang.NullPointerException- You used an object that is
null. Make sure it is created withnewor assigned before calling its methods.
Frequently asked questions
Is this online Java compiler free?
Yes. It is completely free to use, with no signup required to write and run code.
Do I need to install anything?
No. It all works from your web browser, so there is nothing to download or set up — it works on Windows, macOS, Linux, Chromebooks and phones.
How do I give input to my Java program?
Before clicking Run, type the input into the STDIN box under the I/O tab, one value per line. Your program reads it as standard input, just like typing in a terminal.
Is this good for learning Java?
Yes. It is ideal for practising exercises, testing small programs and trying out ideas from tutorials, because you can run code instantly without configuring an environment.
Other online compilers
Python Compiler C Compiler C++ Compiler C# Compiler JavaScript Compiler TypeScript Compiler SQL Compiler React.js Compiler HTML Compiler Go Compiler Rust Compiler R Compiler PHP Compiler Kotlin Compiler Swift Compiler Scala Compiler Dart Compiler Ruby Compiler Prolog Compiler Perl Compiler
Editor powered by OneCompiler.