Prettier for Java

Prettier for Java

Do you spend a lot of time formatting Java files over and over again? What? Unexpected! We too!

We were considering the usual solutions to this: IDE autoformat, git hooks, manual tweaking. At some point, we wondered: do we have anything like Prettier, but for Java?


Prettier Java Plugin. Contribute to jhipster/prettier-java development by creating an account on GitHub.

If you are used to other code formatting tools, be warned: Prettier is opinionated. You get a handful of options, and for everything else the answer is "whatever, as long as I don't have to look at it anymore." The whole team needs to be OK with that – if you don't like this, you should stop reading here.


We found the installation steps a bit confusing, so this is the playbook that we follow:

  • Install using node: npm i --save-dev prettier-plugin-java
  • Create a new .prettierrc.yaml (or edit an existing one) with your own Java preferences:
  - files:
      - '*.java'
      printWidth: 120
      tabWidth: 2
      useTabs: false
  • Run a first sweep to reformat your code: npx prettier --write "src/**/*.java"
  • Check out the result, then commit your changes to git

Configure Prettier in IntelliJ

You can now reformat files using the command line. Great. Let's integrate that with IntelliJ:

  • Modify the Organize imports setting in IntelliJ. Consider sharing this setting by committing .idea/codeStyles/Project.xml to your git repo.
  • If you don't have it yet, install the File Watchers plugin for IntelliJ (it comes included with Webstorm by default)
  • Add a new watcher under Settings > Tools > File Watchers:
Name: Prettier Java
File Type: Java
Program: npx
Arguments: prettier --write $FilePath$
Output path to refresh: $FilePath$
Trigger the watcher on external changes: checked
Autosave edited files to trigger the watcher: deactivated

You may notice that this is slightly different from the documentation.

Make a modification to a Java file, save (or switch to another application and back), and prepare to be inspired by the astonishing beauty of automatically formatted code. Just like that.