{"id":26101,"library":"prettier-plugin-java-for-cjs","title":"Prettier Java Plugin","description":"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.","status":"active","version":"2.8.4","language":"javascript","source_language":"en","source_url":"https://github.com/sunrabbit123/prettier-java","tags":["javascript"],"install":[{"cmd":"npm install prettier-plugin-java-for-cjs","lang":"bash","label":"npm"},{"cmd":"yarn add prettier-plugin-java-for-cjs","lang":"bash","label":"yarn"},{"cmd":"pnpm add prettier-plugin-java-for-cjs","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency required as the plugin runs within Prettier","package":"prettier","optional":false}],"imports":[{"note":"Plugin auto-loads when installed; no explicit import of the plugin is needed. The example uses CommonJS require as shown in docs.","wrong":"import prettier from 'prettier'; formatted = prettier.format(code, { parser: 'java' });","symbol":"default","correct":"const prettier = require('prettier'); const formatted = prettier.format(code, { parser: 'java' });"},{"note":"The plugin does not have a default export; use namespace import. Typically you don't need to import it directly.","wrong":"import prettierPluginJava from 'prettier-plugin-java';","symbol":"plugin as module","correct":"import * as prettierPluginJava from 'prettier-plugin-java';"},{"note":"Plugin auto-detects .java files. The --parser flag is not required and may cause issues if the plugin is not loaded.","wrong":"prettier --parser java --write MyFile.java","symbol":"CLI usage","correct":"prettier --write MyFile.java"}],"quickstart":{"code":"const prettier = require('prettier');\nconst javaCode = `public class HelloWorld { public static void main(String[] args) { System.out.println(\"Hello World!\"); } }`;\nprettier.format(javaCode, { parser: 'java' }).then(result => console.log(result));","lang":"javascript","description":"Showcases formatting a simple Java class using Prettier API with the java parser."},"warnings":[{"fix":"Upgrade Prettier to ^3.0.0. Run: npm install prettier@^3.0.0 --save-dev","message":"prettier-plugin-java v2.x requires Prettier v3.x. Using Prettier v2 will fail to load the plugin.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Upgrade to prettier-plugin-java@^2.0.0 and Prettier@^3.0.0","message":"Prettier v2 support dropped; v1.x of this plugin is no longer maintained.","severity":"deprecated","affected_versions":"<=1.6.2"},{"fix":"Install globally: npm install -g prettier-plugin-java, or reference plugin via --plugin=./node_modules/prettier-plugin-java","message":"Plugin must be installed locally or globally to be auto-loaded by Prettier. If not, you need to pass --plugin path explicitly.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Use one of the valid entrypoints listed in the plugin's options file.","message":"Custom parser option 'entrypoint' only works with certain node types; invalid entrypoint causes formatting to fail silently.","severity":"gotcha","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Run npm install prettier-plugin-java --save-dev","cause":"Plugin not installed or not in node_modules when using require/import.","error":"Error: Cannot find module 'prettier-plugin-java'"},{"fix":"Ensure prettier-plugin-java@^2.0.0 with Prettier@^3.0.0, and install the plugin.","cause":"Plugin not loaded or Prettier version incompatible (v2.x).","error":"Error: Couldn't resolve parser \"java\""},{"fix":"Check the options file for valid entrypoints; use one like 'compilationUnit' or 'classBodyDeclaration'.","cause":"Entrypoint option value not in allowed list.","error":"Error: Invalid entrypoint 'myEntry' for java parser"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}