Logo

MonoCalc

/

Java Prettier

Programming
Runs entirely in your browser
The Java parser and printer are downloaded once and then run locally. Your code is never uploaded, never logged and never compiled or executed, so proprietary source, internal endpoints and a key that slipped into a snippet are safe to paste.
Pretty-prints the source with the options below.
Auto tries file, then class body, then statements

Load a sample:

2 spaces100 cols

Java in

Formatted out

Formatting…

About This Tool

Java Prettier - format and beautify Java in your browser

A Java formatter takes source that has been flattened onto one line, decompiled, hand-indented by three different people or pasted out of a chat window, and prints it again in one consistent style. This one works the way your IDE does: it parses the code into a real concrete syntax tree and prints that tree from scratch, so the layout is decided by the language structure rather than by counting braces. Everything happens inside your own tab - no JDK, no Maven, no Gradle, no upload.

Why an AST-based beautifier matters for Java

Regex beautifiers fall over on modern Java almost immediately. A { inside a text block is not a block. A < in Map<String, List<Integer>> is not a comparison. An arrow in a switch arm is not a lambda. Parsing the file properly is what lets the printer keep records, sealed interfaces and permits clauses, pattern matching for instanceof and switch with when guards, yield, triple-quoted text blocks, var, lambdas, method references, annotations in every position, generics with bounded and wildcard parameters, enum constant bodies, try-with-resources, multi-catch, labelled breaks, static and instance initialisers, and a whole module-info.java intact.

The same parse gives you a free syntax check. Paste something that does not compile and you get the exact line, the exact column and a caret pointing at the token that confused the parser - usually faster than waiting for a build.

The options, and what they actually change

Print width is a target rather than a hard limit: the printer fits what it can on a line and breaks the rest, so dropping it to 40 is the quickest way to watch a builder chain explode into one call per line. Indent style and indent size are the settings teams argue about; the three presets encode the common answers - Google Java Style at two spaces and 100 columns, AOSP at four and 100, Oracle/Sun at four and 80. Brace style switches between attached (K&R) and next-line (Allman) braces. The import and comment switches are the only ones that rewrite the code itself rather than its whitespace, and each is re-parsed before it is accepted.

Modes for different jobs

Format is the everyday mode. Unminify expands a decompiled or single-line blob into browsable structure - no identifier is ever renamed and no logic is altered. Compact removes the column limit so declarations stay on one line, which is what you want for a slide or a chat message. Diff shows exactly which lines the formatter would touch before you commit anything. Validate parses and reports only success or the first syntax error. Compare renders the same file under two presets side by side, so the cost of a style migration is visible before anyone opens the pull request.

Snippets are handled automatically. Leave the parse mode on Auto-detect and paste a bare method, a couple of fields or a run of statements: the tool tries a whole compilation unit first, then a class body, then bare statements, and tells you which one it used.

How the output is checked

Formatting is only allowed to move whitespace, so every run proves it. The printed source is read back by the same lexer and its token stream compared with the input's, token by token. Three liberties the printer is known to take are recognised and named rather than hidden: it re-indents text blocks (without changing the string they denote, which the check verifies using the incidental-whitespace rules from the language specification), it sorts each contiguous block of import declarations, and it may add parentheses around an expression it had to wrap. Anything else is reported as a failure instead of being handed back quietly.

It is a formatter, not a compiler
Valid layout is not valid code. The parser accepts the full modern grammar and says nothing about whether your types resolve, your imports exist or your method actually returns. Set the Java language level to be warned when the source uses syntax newer than the release you target.

Everything stays in your tab

The parser and printer are JavaScript, loaded on first use and run locally. Your source is never uploaded, never logged and never executed. Copy the result, download it as a .java file, or export the run as text or CSV - and take the generated .prettierrc and .editorconfig snippets with you so the same style is reproduced in CI.

Frequently Asked Questions

Is the Java Prettier free?

Yes, Java Prettier is totally free :)

Can I use the Java Prettier offline?

Yes, you can install the webapp as PWA.

Is it safe to use Java Prettier?

Yes, any data related to Java Prettier only stored in your browser (if storage required). You can simply clear browser cache to clear all the stored data. We do not store any data on server.

How does this Java formatter work?

Your source is parsed in the browser into a real concrete syntax tree and then printed again from scratch with the style options you pick. Because the output is generated from the parsed structure rather than by nudging braces around with regular expressions, modern syntax such as records, sealed types, switch expressions, text blocks and lambdas comes out correctly instead of being mangled.

Is my code uploaded anywhere?

No. The parser and the printer are JavaScript that runs inside your own tab, with no server round-trip, no logging and no compilation. Nothing is executed either - the tool only reads your code and prints it back. That matters for Java in particular, where a pasted class routinely carries internal package names, table names, endpoints and the occasional credential nobody remembered to strip.

Can formatting change what my code does?

Printing only moves whitespace, so it cannot. To prove it, every run re-reads the printed output and compares its token stream with the input's, token by token; the badge under the output reports the verdict. Three well-known exceptions are recognised and named rather than hidden: the printer re-indents text blocks without changing their value, it sorts each block of imports, and it may add parentheses around an expression it had to wrap.

Is this the same as google-java-format?

No, and it cannot be - google-java-format is a JVM tool and there is no JVM in a browser tab. The Google Java Style preset here is Prettier emulating that layout: two-space indentation and a 100-column limit. The result is close in spirit and perfectly consistent, but it will not be byte-identical to what the official tool produces, so do not use it to satisfy a CI check that runs the real thing.

Can I format a method or a few statements without a whole class?

Yes. Leave the parse mode on Auto-detect and paste whatever you have. The tool tries a full compilation unit first, then a class body, then a run of bare statements, and reports which one it used. Class-body snippets are parsed inside synthetic braces that are removed again afterwards, so you never have to wrap three lines in a throwaway class by hand.

Why did my `if` body end up on the same line as the condition?

Prettier prints an unbraced single-statement body inline when it fits, because that is what the source said - it never adds braces you did not write. If you want every body braced, add the braces yourself before formatting. Next-line (Allman) braces are available as a post-pass, and each brace it moves is located through the lexer, so a brace inside a string, a char literal, a text block or a comment is never touched.