Java Formatter
Format Java code online with Prettier and prettier-plugin-java. Choose 2 or 4 space indents and line width; runs privately in your browser.
- Free
- No sign-up
- Runs in your browser
Java Formatter
Runs in your browser. Your inputs never leave your device.
In short
Quick Answer
Paste a Java class and press Format. Prettier with prettier-plugin-java puts opening braces on the same line, indents each block by 4 spaces (or 2 for Google style), adds spaces around operators and after commas, and wraps long expressions at the line width you choose. If the code does not parse, for example because of a missing semicolon or brace, it reports the line and column instead.
Step by step
How to Use
- Paste your code. Paste a Java class or file into the input box, or load the example.
- Choose options. Pick 4-space (Oracle) or 2-space (Google) indentation and a print width of 80, 100 or 120.
- Format. Press Format or Ctrl+Enter. If the code cannot be parsed, fix it at the reported line and column.
- Copy the result. Copy the formatted code from the output box.
Java formatting conventions
Java has two widely used style references. Oracle's Code Conventions for the Java Programming Language date from 1999 and are now archived, but most Java code still follows their layout. The Google Java Style Guide is a newer, stricter standard used by many teams and open-source projects. Both agree on the basics this formatter applies:
- Braces on the same line. The opening brace goes at the end of the line that starts the class, method,
if,forortry; the closing brace sits on its own line, aligned with the start of that statement. - Consistent indentation. Oracle's conventions use 4 spaces per level and keep lines under 80 characters. Google Java Style uses 2 spaces per block and a 100-column limit.
- One statement per line, with spaces around binary operators, after commas and after keywords such as
ifandfor. - Wrapped long expressions. Method chains such as streams are broken one call per line, so each step is easy to read.
Prettier's Java style is close to both, but it is its own consistent style: it chooses line breaks automatically and does not try to reproduce every detail of either guide. Use 4 spaces and 80 columns for an Oracle-like layout, or 2 spaces and 100 columns for a Google-like one.
Naming conventions and imports
A formatter changes layout only, so naming is still up to you. The usual Java conventions are UpperCamelCase for classes and interfaces (OrderTotal), lowerCamelCase for methods and variables (calculateTotal, itemCount), UPPER_SNAKE_CASE for constants (MAX_RETRIES), and all-lowercase package names (com.example.billing).
Imports are left in the order you wrote them, and unused imports are not removed. Google Java Style puts all static imports in one block and all non-static imports in another, each in ASCII order, with no wildcard imports. Most IDEs can organise imports this way for you.
When the code will not format
Prettier has to parse your code before it can print it again. If the parser cannot make sense of it, the formatter does not guess: it reports the line and column of the first missing or unexpected token, and the cursor jumps there in the input. The usual causes are:
- A missing semicolon at the end of a statement, reported as a missing
;right after it. - An unbalanced brace or parenthesis. A missing closing brace is often reported at the end of the code, where the parser runs out of input.
- A fragment outside a class. Statements need to be inside a method, and methods inside a class.
Private by design
This page runs Prettier with prettier-plugin-java, which parses Java with a tree-sitter grammar compiled to WebAssembly. All of it is served from this site and downloaded only when you first format code on this page; after that it runs in your browser, so your source code never leaves your device. For Python, use the Python Formatter.
Examples
Worked Examples
A class written on one line
Before
public class OrderTotal{private final List<Integer> prices;public OrderTotal(List<Integer> prices){this.prices=prices;}public int total(){int sum=0;for(int p:prices){sum+=p;}return sum;}}
After
public class OrderTotal {
private final List<Integer> prices;
public OrderTotal(List<Integer> prices) {
this.prices = prices;
}
public int total() {
int sum = 0;
for (int p : prices) {
sum += p;
}
return sum;
}
}
Result: Each member moves to its own line, braces open on the same line as the declaration, blocks are indented by 4 spaces, and a blank line separates the members.
A for loop with an if inside
Before
class Report{void print(List<String> items){for(int i=0;i<items.size();i++){if(items.get(i).isEmpty()){continue;}System.out.println(i+": "+items.get(i));}}}
After
class Report {
void print(List<String> items) {
for (int i = 0; i < items.size(); i++) {
if (items.get(i).isEmpty()) {
continue;
}
System.out.println(i + ": " + items.get(i));
}
}
}
Result: The loop header gets spaces after each semicolon and around operators, and the body is expanded onto separate, indented lines.
A stream with lambdas
Before
class Adults{List<String> names(List<Person> people){return people.stream().filter(p->p.getAge()>=18).map(p->p.getName().toUpperCase()).sorted().collect(Collectors.toList());}}
After
class Adults {
List<String> names(List<Person> people) {
return people
.stream()
.filter(p -> p.getAge() >= 18)
.map(p -> p.getName().toUpperCase())
.sorted()
.collect(Collectors.toList());
}
}
Result: The chain is too long for one line, so each call moves to its own line, and each lambda gets spaces around the arrow and the comparison.
Watch out
Common Mistakes to Avoid
- Missing semicolon or brace: Java cannot be parsed without its semicolons and matching braces, so the formatter stops and reports where the parser expected one. A missing closing brace is often reported at the end of the file, because that is where the parser runs out of code.
- Pasting a fragment instead of a class: Prettier formats complete Java source. If you paste only a few statements, wrap them in a class and method (for example class Example { void run() { ... } }) so they can be parsed.
- Mixing 2-space and 4-space projects: Oracle's conventions use 4 spaces and Google Java Style uses 2. Choose the indent your project already uses, or every file you format will show whitespace-only changes in review.
- Expecting imports to be sorted or removed: The formatter keeps imports in the order you wrote them and does not remove unused ones. Use your IDE's organise-imports action or a linter for that.
- Expecting names to be fixed: Formatting never renames classes, methods or variables. Java naming conventions, such as UpperCamelCase classes and lowerCamelCase methods, are up to you and your linter.
FAQ
Frequently Asked Questions
How do I format Java code online?
Paste your Java class into the box, choose an indent and line width, and press Format or Ctrl+Enter. The formatted code appears below, ready to copy. It runs in your browser, so your code is not uploaded.
Should Java use 2 or 4 spaces for indentation?
Both are common. Oracle's original Java code conventions use 4 spaces, and the Google Java Style Guide uses 2 spaces with a 100-column limit. Use whatever your project already uses.
What is the Google Java Style Guide line length?
Google Java Style uses a column limit of 100 characters. Choose 2 spaces and a print width of 100 here to follow it.
Where do opening braces go in Java?
On the same line as the class, method or statement that opens the block, with the closing brace on its own line. Both the Oracle conventions and Google Java Style use this layout, and the formatter applies it.
Why does the formatter say the code could not be parsed?
The code is not valid Java, often because of a missing semicolon, an unbalanced brace or parenthesis, or a fragment that is not inside a class. The error shows the line and column where parsing failed.
Does the formatter sort my imports?
No. Imports keep the order you wrote them. Most IDEs can organise imports for you, and Google Java Style puts static imports in one group and non-static imports in another, each in ASCII order.
Is my code uploaded to a server?
No. Prettier, its Java plugin and the Java grammar are served from this site and run in your browser after they download the first time you format.
References