Output appears here.
The Java Formatter reformats Java source with Prettier for Java: classes, records, sealed interfaces, lambdas, switch expressions and stream chains get consistent indentation and line breaks. It is for developers cleaning up a pasted class, a Stack Overflow snippet or generated code without opening an IDE. Everything runs in your browser — nothing is uploaded.
How to format Java code
- Paste your code, open a
.javafile with ⌘/Ctrl O, or load an example. A whole file, a single class or just a method body all work. - Set Tab width to 4 (the Java convention and the default) or 2, and Print width to your team's line limit.
- Copy the result with ⌥/Alt C or download it with ⌘/Ctrl S.
Examples
A class on one line
Members are split onto their own lines, operators get spaces, and a blank line separates each member.
public class Greeter{private final String name;public Greeter(String name){this.name=name;}public String greet(boolean excited){return "Hello, "+name+(excited?"!":".");}public static void main(String[] args){System.out.println(new Greeter("World").greet(true));}}Result:
public class Greeter {
private final String name;
public Greeter(String name) {
this.name = name;
}
public String greet(boolean excited) {
return "Hello, " + name + (excited ? "!" : ".");
}
public static void main(String[] args) {
System.out.println(new Greeter("World").greet(true));
}
}Record and a stream pipeline
Records stay compact, and a stream chain whose calls take lambdas and method references is broken one call per line.
record Order(long id,double total){}
class Report{static double sum(java.util.List<Order> o){return o.stream().filter(x->x.total()>0).mapToDouble(Order::total).sum();}}Result:
record Order(long id, double total) {}
class Report {
static double sum(java.util.List<Order> o) {
return o
.stream()
.filter(x -> x.total() > 0)
.mapToDouble(Order::total)
.sum();
}
}Spring controller with a switch expression
Annotations move onto their own lines, and arrow-style case labels are indented inside the switch.
@RestController @RequestMapping("/api/orders") public class OrderController{
@Autowired private OrderService service;
String label(Status s){return switch(s){case PAID->"Paid";case SHIPPED,DELIVERED->"On its way";default->"Pending";};}}Result:
@RestController
@RequestMapping("/api/orders")
public class OrderController {
@Autowired
private OrderService service;
String label(Status s) {
return switch (s) {
case PAID -> "Paid";
case SHIPPED, DELIVERED -> "On its way";
default -> "Pending";
};
}
}Options explained
- Print width — the line length the formatter tries to stay within before wrapping parameters, arguments and binary expressions (80, 100 or 120).
- Tab width — spaces per indentation level; 4 matches the usual Java convention, 2 matches Google Java Style's block indent.
Troubleshooting Java parse errors
A parse failure reads "Syntax error: missing …" and names the token the parser expected, with the hint "The Java parser expected a token that is missing (often a ; or a closing brace)." The position is not always the exact line of the mistake, so read the message first.
Syntax error: missing ;
A statement or field declaration has no semicolon, as in int x = 1 int y = 2;. Look for a declaration or statement whose line does not end in ;.
Syntax error: missing }
A class, method or block is not closed. Each } listed in the message is one unclosed brace — "missing }, }" means two — so count them to see how many are needed at the end.
Syntax error: missing identifier
A declaration has a type but no name, such as int = 5;, or a keyword is being used as a variable name.
The input is Kotlin, Groovy or Scala
Code from other JVM languages does not parse as Java; val x = 1 fails with "Syntax error: missing ;", for example. Only Java source is supported.
Method chains are split more than I expected
Prettier for Java breaks longer member chains one call per line, particularly when the calls take lambdas or method references, even when the chain would fit within the print width. Short chains such as list.stream().map(f) stay inline. This is intentional, keeps fluent APIs readable in diffs, and does not change with a wider print width.
FAQ
Does it follow Google Java Style or the Eclipse/IntelliJ defaults?
It follows Prettier for Java's own style, which is close to common IDE defaults but not identical to any of them. Braces stay on the same line, members are separated by blank lines, and long chains break per call. If your build enforces google-java-format or a Checkstyle configuration, use those tools for the final check; this formatter is for quick, consistent cleanup.
Which Java versions are supported?
Modern syntax parses and formats correctly, including var, records, sealed interfaces with permits, switch expressions with arrow labels, record patterns in case labels, text blocks and generics. Text block contents are moved with the surrounding indentation, which keeps the string's value unchanged because Java strips the common indent relative to the closing delimiter.
Can I format a method or a few statements without a class?
Yes. A single method such as public int add(int a,int b){return a+b;} or a bare statement like System.out.println("hi"); is accepted and formatted on its own. That makes it convenient for tidying a snippet before pasting it into documentation, a code review comment or a ticket.
Does it organise imports?
It sorts the import statements it finds, so java.io.File moves above java.util.*, and it keeps a blank line after the package declaration. It does not add missing imports, remove unused ones or expand wildcard imports; that needs an IDE or a compiler-aware tool.
Is my source code uploaded?
No. Prettier and its Java plugin run in your browser inside an isolated frame that blocks network requests, so nothing is uploaded or stored on a server. Your print width and tab width are remembered on this device, and the code itself is kept only if you turn on "Remember my last input".
Why is there a blank line after the class's opening brace?
Prettier for Java separates every class member with a blank line and treats the first member the same way, so a blank line appears directly after {. Empty classes and records such as record Order(long id) {} stay on one line. This spacing is part of the plugin's fixed style and has no setting.
Related formatters
Java projects often sit next to other languages. The C Formatter and C++ Formatter use clang-format for native code, the Python Formatter covers build and data scripts, and the full set of code tools includes formatters for JavaScript, TypeScript and GraphQL. The Prettier options shared by these tools are described in Prettier defaults explained.