Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Introduction

Agreeing on code styling within a team can be challenge. Therefore, automatic code formatters are often a time saver when it comes to code readability and best practices synchronization.

In this tutorial, we’ll explore Java code formatting with prettier in IntelliJ IDEA.

2. Prettier and Java Support

prettier is a popular opinionated code formatter. Unlike Java formatting tools, it’s not IDE, language, or platform specific. Thus, we can use the same tool for multiple parts of a large project, simplify workflows and enhance productivity.

Although Java isn’t natively supported, prettier offers additional language formatting through plugins. Some plugins are maintained by the prettier team, some by the community. For Java, we have a community plugin called prettier-java.

3. Installation

Since prettier is an npm package, we should make sure Node is installed along with npm. To validate, let’s check the versions:

$ node --version
v24.13.0
$ npm --version
11.6.2

It’s a best practice to install npm packages locally, to make sure there are no version conflicts with other projects, and everyone on the team uses the same versions of the packages.

Next, we create a package.json file by running npm init:

$ npm init -y

This way, we create the file by accepting all default values. In this case, -y is short for –yes.

Next, we install two packages – prettier and prettier-java plugin:

$ npm install --save-dev --save-exact prettier prettier-plugin-java

Here, –save-dev saves the packages as development dependency. While –save-exact saves the current version number without any version operators.

The resulting package.json file should have a specific structure:

{
  "name": "java-test-project",
  "version": "1.0.0",
  "description": "",
  "license": "ISC",
  "author": "",
  "type": "commonjs",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "devDependencies": {
    "prettier": "3.8.1",
    "prettier-plugin-java": "2.8.1"
  }
}

Notably, prettier-java requires at least Node v10.

4. Setup in IntelliJ IDEA

Now, let’s see how to set up prettier as an external tool in IntelliJ. This way, we have the most configuration flexibility, but may need to spend more time.

However, external tool setup isn’t the only option. Starting v2020.1, IntelliJ natively supports prettier. For older versions, we have an IntelliJ plugin. However, the convenience of a plugin or built-in tool comes with restrictions in terms of configuration and versioning.

In this setup, we use IntelliJ IDEA Community Edition v2025.3 on a Windows system.

4.1. Creating a Prettier Config File

In the project root directory, we create a prettier config file named .prettierrc.json with the Java plugin setup:

{
  "tabWidth": 2,
  "plugins": ["prettier-plugin-java"]
}

Here, we’ve also configured tabWidth to be two spaces. Additional configurations can be added as necessary.

4.2. Configuring External Tool

To configure an external tool in IntelliJ, we go to File > Settings > Tools External Tools. Then, on the sub-window toolbar, we click on the Add button. A pop-up window should open:

Intellij external tools setup pop-up

In this pop-up, let’s set the name to Prettier and skip the description.

We could use prettier’s executable path for this configuration. However, because there’s no executable file in the node_modules\prettier\bin directory on Windows, only a .cjs file, we provide a platform-independent solution. Specifically, let’s use npx, which comes with npm, to execute the locally installed prettier.

Therefore, we fill in the Program field with npx executable path C:\Program Files\nodejs\npx.cmd.

Afterwards, we set up calling prettier in the Arguments field:

prettier --config $ProjectFileDir$\.prettierrc.json --write $FilePathRelativeToProjectRoot$

$FilePathRelativeToProjectRoot$ references the currently opened file, while –write tells the command to output changes directly to the specified file. So, when run, this tool formats the open file in place.

Finally, we set the Working directory field to $ProjectFileDir$. Then, let’s click the OK button on the pop-up window and the Apply button in the settings window in the end.

4.3. Running

Now that we’re done with the setup, we can open any Java file from the project and run the new tool. Of course, let’s make sure the code has styling inconsistencies for prettier to fix.

Next, we go to Tools > External Tools and select the newly created Prettier tool to run it:

Intellij external tools run menu

This should execute the command and fix the styling issues in place. The terminal section of the IDE should show execution details.

To trigger the tool faster, we can create a custom IntelliJ shortcut.

5. Additional Notes

The setup is similar in other popular development environments, namely Eclipse and Visual Studio Code, with the option of external tool configuration or a plugin. Eclipse’s prettier plugin, however, is outdated and no longer maintained.

One issue to be aware of, we can end up with version mismatches and a non-working setup because of multiple layers of independent tools, such as prettier, community plugin, IDE plugin. So, it’s important to pin the exact versions when we have a working solution.

In addition, because the Java plugin is maintained by the community, it usually stays behind language updates. It’s a common annoyance when working with non-native tools.

6. Summary

In this article, we’ve looked into how to manually set up and use prettier for Java in IntelliJ IDEA. This should give us transparency and full control over the configuration process.

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW