{"id":20376,"library":"prettier-plugin-java","title":"Prettier Java Plugin","description":"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.","status":"active","version":"2.8.1","language":"javascript","source_language":"en","source_url":"https://github.com/jhipster/prettier-java","tags":["javascript"],"install":[{"cmd":"npm install prettier-plugin-java","lang":"bash","label":"npm"},{"cmd":"yarn add prettier-plugin-java","lang":"bash","label":"yarn"},{"cmd":"pnpm add prettier-plugin-java","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency: plugin requires Prettier >=3.0.0 to run.","package":"prettier","optional":false}],"imports":[{"note":"Prettier plugins are usually auto-discovered if installed. For programmatic usage, pass plugin in options.","wrong":"const plugin = require('prettier-plugin-java') and passing to Prettier API incorrectly","symbol":"(plugin, auto-loaded)","correct":"Add plugin to .prettierrc: { \"plugins\": [\"prettier-plugin-java\"] } or use --plugin=prettier-plugin-java"},{"note":"The plugin has no default export. It is used implicitly by Prettier.","symbol":"default (none)","correct":"No explicit import needed; just install and configure in .prettierrc"},{"note":"TypeScript types are bundled with the package. No separate type import needed.","wrong":"import something from 'prettier-plugin-java/types' (does not exist)","symbol":"(types)","correct":"import { Plugin } from 'prettier'; // types for plugin options"}],"quickstart":{"code":"// 1. Install\n// npm install --save-dev prettier prettier-plugin-java\n\n// 2. Configure .prettierrc\n{\n  \"semi\": true,\n  \"singleQuote\": true,\n  \"tabWidth\": 4,\n  \"trailingComma\": \"all\",\n  \"plugins\": [\"prettier-plugin-java\"]\n}\n\n// 3. Run\n// npx prettier --write \"src/**/*.java\"\n\n// Or programmatically:\nimport * as prettier from 'prettier';\nconst code = `public class Hello { public static void main(String[] args) { System.out.println(\"Hello\"); } }`;\nconst formatted = await prettier.format(code, {\n  parser: 'java',\n  plugins: ['prettier-plugin-java']\n});\nconsole.log(formatted);","lang":"typescript","description":"Shows installation, Prettier config with plugin, CLI usage, and programmatic formatting with Java parser."},"warnings":[{"fix":"Upgrade Prettier to v3 or later, or use prettier-plugin-java v1.x with Prettier v2.","message":"Version 2.x requires Prettier >=3.0.0. Not compatible with Prettier v2.","severity":"breaking","affected_versions":"<2.0.0"},{"fix":"If you rely on 'avoid', set explicitly in config.","message":"Option 'arrowParens' default changed to 'always' in v2.7.3 to align with Prettier.","severity":"deprecated","affected_versions":">=2.7.3"},{"fix":"Add plugins array in .prettierrc or pass --plugin=prettier-plugin-java.","message":"Plugin auto-loads if installed in the same project. But if using global Prettier, must specify --plugin or in config.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Use 'semi' option to control semicolons, not 'trailingComma'.","message":"Trailing semicolon placement: the plugin does NOT connect trailing semicolon to prettier's 'trailingComma' option (reverted in v2.7.4).","severity":"gotcha","affected_versions":">=2.7.4"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Run 'npm install --save-dev prettier prettier-plugin-java'.","cause":"Plugin not installed or not in node_modules.","error":"Cannot find module 'prettier-plugin-java'"},{"fix":"Ensure you are formatting a .java file or specify parser: 'java' in programmatic API.","cause":"Using .prettierrc without specifying parser and Prettier cannot auto-detect .java file.","error":"Error: No parser and no file path given, couldn't infer a parser."},{"fix":"Upgrade Prettier to v3: 'npm install prettier@latest'.","cause":"Using incompatible Prettier version (v2) with plugin v2.","error":"TypeError: prettier.resolveConfig.sync is not a function"},{"fix":"Check that the plugin is installed in the same project as Prettier and that Prettier >=3.","cause":"Plugin is not a valid Prettier plugin (e.g., installed but not recognized).","error":"Error: Invalid plugin: prettier-plugin-java"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}