Formatting Java Code with Prettier
Consistent code formatting is crucial for readability, maintainability, and collaboration in Java projects. While IDEs like IntelliJ IDEA offer built-in formatting, Prettier provides an automated, opinionated approach to enforcing consistent formatting across different editors, codebases, and CI/CD pipelines. This ensures that all team members follow the same style, reducing unnecessary formatting discussions during code reviews.
1. Introduction to Prettier for Java
Prettier is a widely used code formatter initially designed for JavaScript, TypeScript, HTML, and CSS. Over time, the community has extended Prettier’s support to Java via the prettier-plugin-java. This plugin allows developers to automatically format Java code in a consistent style, regardless of the IDE or editor being used.
- Consistency: Ensures all code looks the same across a project.
- Automation: Reduces manual formatting effort.
- Integration: Works with multiple editors, Git hooks, and CI/CD pipelines.
2. Installing Prettier and the Java Plugin
To use Prettier for Java, you first need Node.js installed, since Prettier runs on Node. Then, install Prettier and the Java plugin either globally or locally in your project.
// Install Node.js first from https://nodejs.org/ // Install Prettier globally npm install -g prettier // Install Prettier and the Java plugin locally in your project npm install --save-dev prettier prettier-plugin-java
Tip: Installing locally (--save-dev) ensures that all team members use the same Prettier version, avoiding formatting inconsistencies.
3. Configuring Prettier in IntelliJ IDEA
To integrate Prettier with IntelliJ IDEA for Java projects:
- Go to File → Settings → Plugins and search for the Prettier plugin.
- Install the plugin and restart the IDE.
- Navigate to File → Settings → Languages & Frameworks → Prettier and configure the Prettier executable path (global or local project path).
- Enable the On save option to automatically format Java files whenever you save.
- Optionally, use File Watchers to run Prettier on certain file types or during build processes.
4. Additional Configuration Notes
Prettier enforces an opinionated style, which may differ from your IDE’s default formatting. You can customize some formatting options using a .prettierrc configuration file:
{
"tabWidth": 4, // Number of spaces per indentation level
"useTabs": false, // Use spaces instead of tabs
"semi": true, // Add semicolons at the end of statements
"singleQuote": true, // Use single quotes for strings
"printWidth": 100 // Maximum line width before wrapping
}
- Prettier supports formatting on save, pre-commit hooks, and CI pipelines.
- The Java plugin respects syntax including annotations, generics, and lambda expressions.
- Some IDE-specific formatting (like auto-import reordering) may need to be combined with Prettier.
5. Java Code Formatting Example
Unformatted Java program:
public class HelloWorld {
public static void main(String[] args){
System.out.println("Hello, Prettier World!");
int sum=addNumbers(5, 10);
System.out.println("Sum: "+sum);
}
public static int addNumbers(int a,int b){
return a+b;
}
}
After running Prettier using:
prettier --write "**/*.java"
The code is formatted as:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Prettier World!");
int sum = addNumbers(5, 10);
System.out.println("Sum: " + sum);
}
public static int addNumbers(int a, int b) {
return a + b;
}
}
- Proper indentation (4 spaces) and spacing around operators.
- Consistent placement of braces.
- Enhanced readability and maintainability.
Output when executed:
Hello, Prettier World! Sum: 15
6. Best Practices and Recommendations
- Combine with Git hooks: Use Husky to run Prettier before commits to ensure all code is formatted.
- Pair with ESLint or Checkstyle: For enforcing additional Java code rules beyond formatting.
- Version control: Commit your
.prettierrcto share formatting rules across the team. - Editor support: Most editors (VS Code, IntelliJ IDEA, WebStorm) have Prettier plugins for on-the-fly formatting.
7. Conclusion
Using Prettier for Java ensures consistent and maintainable code across your team and environments. By integrating it with IntelliJ IDEA and leveraging the prettier-plugin-java, developers can automate formatting, reduce style-related debates in code reviews, and focus on writing quality Java logic. Adopting Prettier encourages clean, readable, and standardized code that scales well for large projects.

