Java Code Formatter
Format and beautify Java source code — re-indent by brace depth, normalize operator spacing, choose K&R or Allman brace style, and sort & group imports (java, javax, third-party, static).
Input
Formatting Options
Output
Guides
Paste messy Java and get clean, consistently indented source back instantly. The Java Code Formatter re-indents your code by brace depth, tidies spacing around operators and punctuation, applies the brace style you choose, and can sort and group your import statements — all in your browser, with nothing uploaded to a server.
It is built for the moments a full IDE is overkill: a snippet pasted from a chat, a Stack Overflow answer with collapsed whitespace, generated code with no indentation, or a quick tidy-up before dropping a class into a code review.
How to use it
- Paste your Java source into the input box, or upload a
.javafile. - Pick your Indent (2, 4, or 8 spaces, or tabs) and Brace Style.
- Toggle Sort imports and Normalize blank lines to taste.
- Copy the formatted result, or download it as
formatted.java.
Formatting runs automatically as you type or change an option, so you see the result immediately.
Formatting options
Indent controls the unit used for each nesting level. Spaces are the default (Google's Java style uses 2, the older Oracle conventions use 4); tabs are available if your project prefers them.
Brace Style switches between the two common conventions:
- End of line (K&R) — the opening brace stays on the same line as the declaration, e.g.
public void run() {. - Next line (Allman) — the opening brace moves to its own line beneath the declaration.
Sort and group imports alphabetizes your import statements and groups them into java.*, javax.*, third-party, and static imports, separated by blank lines — the ordering most Java style guides expect.
Normalize blank lines collapses long runs of consecutive blank lines so the output stays compact and readable.
Does it understand strings and comments?
Yes. A Java-aware tokenizer scans the source before anything is reformatted, so braces, semicolons and operators that appear inside string literals, text blocks, character literals, line comments or block comments are never touched. Generics such as List<String> are recognized and left intact, rather than being mistaken for less-than / greater-than comparisons.
Is it the same as running a real formatter?
It is a fast, dependency-free beautifier — it re-indents and spaces your code, but it does not wrap long lines, reflow argument lists, or enforce a complete style specification the way google-java-format or your IDE's formatter would. For a quick, reliable cleanup of indentation, spacing and imports it is ideal; for strict CI-enforced formatting, keep using your build's formatter.
Is my code private?
Completely. All formatting happens locally in your browser using JavaScript. Your source code is never sent to a server, logged, or stored.
Which Java versions are supported?
The tokenizer recognizes modern Java syntax including var, records, sealed classes, text blocks and lambda arrows, so code from Java 8 through the latest releases formats correctly.