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.
Common errors
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.
Warnings
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'.
Install
npm install prettier-plugin-java yarn add prettier-plugin-java pnpm add prettier-plugin-java Imports
- (plugin, auto-loaded) wrong
const plugin = require('prettier-plugin-java') and passing to Prettier API incorrectlycorrectAdd plugin to .prettierrc: { "plugins": ["prettier-plugin-java"] } or use --plugin=prettier-plugin-java - default (none)
No explicit import needed; just install and configure in .prettierrc - (types) wrong
import something from 'prettier-plugin-java/types' (does not exist)correctimport { Plugin } from 'prettier'; // types for plugin options
Quickstart
// 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);