Prettier Java Plugin

raw JSON →
2.8.4 verified Fri May 01 auth: no javascript

Prettier plugin that adds support for formatting Java code. Current stable version is 2.8.4, released with a maintenance cadence. It uses java-parser (Chevrotain-based) to parse Java into a CST and applies Prettier's formatting rules. Key differentiators: integrates seamlessly with Prettier CLI and API, supports custom entrypoints for formatting code snippets, and enforces consistent Java code style across projects. Compatible with Prettier 3.x only.

error Error: Cannot find module 'prettier-plugin-java'
cause Plugin not installed or not in node_modules when using require/import.
fix
Run npm install prettier-plugin-java --save-dev
error Error: Couldn't resolve parser "java"
cause Plugin not loaded or Prettier version incompatible (v2.x).
fix
Ensure prettier-plugin-java@^2.0.0 with Prettier@^3.0.0, and install the plugin.
error Error: Invalid entrypoint 'myEntry' for java parser
cause Entrypoint option value not in allowed list.
fix
Check the options file for valid entrypoints; use one like 'compilationUnit' or 'classBodyDeclaration'.
breaking prettier-plugin-java v2.x requires Prettier v3.x. Using Prettier v2 will fail to load the plugin.
fix Upgrade Prettier to ^3.0.0. Run: npm install prettier@^3.0.0 --save-dev
deprecated Prettier v2 support dropped; v1.x of this plugin is no longer maintained.
fix Upgrade to prettier-plugin-java@^2.0.0 and Prettier@^3.0.0
gotcha Plugin must be installed locally or globally to be auto-loaded by Prettier. If not, you need to pass --plugin path explicitly.
fix Install globally: npm install -g prettier-plugin-java, or reference plugin via --plugin=./node_modules/prettier-plugin-java
gotcha Custom parser option 'entrypoint' only works with certain node types; invalid entrypoint causes formatting to fail silently.
fix Use one of the valid entrypoints listed in the plugin's options file.
npm install prettier-plugin-java-for-cjs
yarn add prettier-plugin-java-for-cjs
pnpm add prettier-plugin-java-for-cjs

Showcases formatting a simple Java class using Prettier API with the java parser.

const prettier = require('prettier');
const javaCode = `public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } }`;
prettier.format(javaCode, { parser: 'java' }).then(result => console.log(result));