Prettier Java Plugin

raw JSON →
2.8.1 verified Sat Apr 25 auth: no javascript

Prettier Plugin for Java is a code formatter that integrates with Prettier to format Java source code. Version 2.8.1 (active development) is released frequently with bug fixes and improvements. It supports Java 8 through 21+, including records, sealed classes, pattern matching, and text blocks. Differentiators: uses Prettier's infrastructure for consistent formatting across languages, respects existing Prettier config, and offers Java-specific options like arrowParens and experimentalOperatorPosition. Requires Prettier v3+ as a peer dependency. Maintained by the JHipster team.

error Cannot find module 'prettier-plugin-java'
cause Plugin not installed or not in node_modules.
fix
Run 'npm install --save-dev prettier prettier-plugin-java'.
error Error: No parser and no file path given, couldn't infer a parser.
cause Using .prettierrc without specifying parser and Prettier cannot auto-detect .java file.
fix
Ensure you are formatting a .java file or specify parser: 'java' in programmatic API.
error TypeError: prettier.resolveConfig.sync is not a function
cause Using incompatible Prettier version (v2) with plugin v2.
fix
Upgrade Prettier to v3: 'npm install prettier@latest'.
error Error: Invalid plugin: prettier-plugin-java
cause Plugin is not a valid Prettier plugin (e.g., installed but not recognized).
fix
Check that the plugin is installed in the same project as Prettier and that Prettier >=3.
breaking Version 2.x requires Prettier >=3.0.0. Not compatible with Prettier v2.
fix Upgrade Prettier to v3 or later, or use prettier-plugin-java v1.x with Prettier v2.
deprecated Option 'arrowParens' default changed to 'always' in v2.7.3 to align with Prettier.
fix If you rely on 'avoid', set explicitly in config.
gotcha Plugin auto-loads if installed in the same project. But if using global Prettier, must specify --plugin or in config.
fix Add plugins array in .prettierrc or pass --plugin=prettier-plugin-java.
gotcha Trailing semicolon placement: the plugin does NOT connect trailing semicolon to prettier's 'trailingComma' option (reverted in v2.7.4).
fix Use 'semi' option to control semicolons, not 'trailingComma'.
npm install prettier-plugin-java
yarn add prettier-plugin-java
pnpm add prettier-plugin-java

Shows installation, Prettier config with plugin, CLI usage, and programmatic formatting with Java parser.

// 1. Install
// npm install --save-dev prettier prettier-plugin-java

// 2. Configure .prettierrc
{
  "semi": true,
  "singleQuote": true,
  "tabWidth": 4,
  "trailingComma": "all",
  "plugins": ["prettier-plugin-java"]
}

// 3. Run
// npx prettier --write "src/**/*.java"

// Or programmatically:
import * as prettier from 'prettier';
const code = `public class Hello { public static void main(String[] args) { System.out.println("Hello"); } }`;
const formatted = await prettier.format(code, {
  parser: 'java',
  plugins: ['prettier-plugin-java']
});
console.log(formatted);