Skip to content
ToolHive

Java Formatter

Format Java in your browser with clang-format compiled to WebAssembly, using the Google Java style. Classes, generics, annotations, lambdas and string literals are handled by the real engine.

Programming Language Tools

How to use Java Formatter

  1. Paste Java source into the input panel.
  2. Choose 2 spaces, 4 spaces or tabs — Google's Java style uses 2.
  3. Press Format. The engine loads on first use, then runs instantly.
  4. Copy the result or download it as Formatted.java.

Examples

Before

A stream pipeline with no line breaks.

public List<String> sorted(Scorer s){return items.stream().sorted(Comparator.comparingDouble(s::score).reversed()).collect(Collectors.toList());}

After

Wrapped at the column limit with continuation indents.

public List<String> sorted(Scorer s) {
  return items.stream()
        .sorted(Comparator.comparingDouble(s::score).reversed())
        .collect(Collectors.toList());
}

Features

  • The real clang-format, compiled to WebAssembly and running locally
  • Google Java style as the base, with a 100-column limit
  • 2-space, 4-space or tab indentation
  • Handles classes, interfaces, enums, generics, annotations and lambdas
  • Comments, string literals and text blocks are preserved
  • Copy to clipboard or download Formatted.java
  • Autosaves your draft to this browser only
  • The engine downloads only when you press Format

Frequently asked questions

Is this google-java-format?
No — it is clang-format using its Google style for Java. The two produce very similar results for ordinary code, but google-java-format is a separate tool and its output can differ in wrapping decisions.
Why is the default 2 spaces?
Because the Google Java style uses two, and that is the base style here. Four spaces is one click away if your project follows the Oracle conventions instead.
Does it handle annotations and generics?
Yes. Annotations are placed on their own lines where the style requires, and nested generic parameters are wrapped rather than run past the column limit.
Does it compile my code?
No. It formats a token stream, so it will happily tidy a method fragment that would not compile on its own.
Will it reorganise my imports?
No. Import ordering is a separate concern that different projects handle differently, and reordering them could change behaviour with static imports.
Is my code uploaded?
No. Formatting happens entirely in your browser.

Related tools

About Java Formatter

Java Formatter is part of Programming Language Tools on ToolHive, a growing collection of small, focused utilities for developers, designers and students. Formatters and playgrounds for Python, C, Java, Go, Rust and SQL. Everything runs client-side, so your data never leaves the browser tab — which makes these tools safe to use with work-in-progress code and private content.