Automating Java Code Cleanup with Eclipse Save Actions
Learn how to configure Eclipse Save Actions to automatically format code and organize imports on save, ensuring team-wide consistency via project-specific settings.
26 Jul 2026, 04:48 UTC

Eliminating Manual Formatting and Import Cleanup
Eclipse Save Actions automate the tedious process of cleaning up source code every time you save a file (Ctrl+S or Cmd+S). Instead of manually triggering a format or organizing imports before a commit, Save Actions apply a predefined set of rules—such as removing unused imports, fixing indentation, and adding missing annotations—immediately before the incremental compiler runs. This ensures that the code in your editor and the resulting bytecode are always consistent and compliant with your team's style guide.
Global Configuration
To enable these actions for all projects in your current workspace:
- Navigate to Window → Preferences → Java → Editor → Save Actions.
- Check Perform the selected actions on save.
- Select the desired cleanup operations:
- Format source code: Applies the active profile from Java → Code Style → Formatter.
- Organize imports: Removes unused imports and sorts the remaining ones based on Java → Code Style → Organize Imports.
- Additional actions: Specific fixes like Add missing @Override annotations or Remove trailing whitespace.
- Click Apply and Close.
Enforcing Team Standards via Project Settings
Global preferences are user-specific. To ensure every developer on a project uses the same formatting and cleanup rules, you must use project-specific overrides and commit them to version control.
- Right-click the project in the Project Explorer → Properties → Java Editor → Save Actions.
- Check Enable project specific settings.
- Configure the actions to match the team standard.
- Click Apply and Close.
Eclipse persists these settings in the .settings/ folder. To maintain consistency across the team, commit the following files to your repository:
.settings/org.eclipse.jdt.ui.prefs: Contains the enablement status and specific action IDs..settings/org.eclipse.jdt.core.prefs: Contains the formatter profile and import organization rules.
Example: Distributing Configuration via .epf
If you cannot commit the .settings/ folder, you can export a preference file (.epf) for manual import by teammates.
Exporting:
Go to File → Export → General → Preferences. Select Java → Editor → Save Actions (and optionally Formatter and Organize Imports). Save the file as team-style.epf.
Importing:
Teammates run File → Import → General → Preferences, select the team-style.epf file, and click Finish.
How it Works: The Execution Pipeline
When a save event is triggered, the Eclipse Java builder invokes the Save Actions participant. This participant uses the JDT (Java Development Tools) AST (Abstract Syntax Tree) rewriting API to modify the source code. The sequence is as follows:
- Trigger: User saves the file.
- AST Rewrite: Save Actions analyze the code structure and apply edits (e.g., removing an unused import).
- Document Update: The editor's text is updated with the cleaned source.
- Compilation: The incremental compiler processes the modified source into
.classfiles.
If Project → Build Automatically is disabled, the Save Actions still run on save, but the compilation step is deferred until a manual build is triggered.
Common Pitfalls and Limitations
Reflection-Based Imports
The Organize Imports action may remove classes that are only referenced via reflection or in configuration files (like JPA entities referenced in persistence.xml). To prevent this, go to Java → Code Style → Organize Imports and add those specific classes to the Never organize imports for list.
Lombok Conflicts
The Add missing @Override action can sometimes conflict with Lombok-generated methods. If you see spurious compilation errors on methods you didn't write, disable the Add missing @Override action in the Save Actions menu.
Performance Latency
On very large files (several thousand lines), running multiple cleanup actions can introduce a perceptible lag (50–300ms) during the save operation. If this occurs, disable expensive actions like Add missing @Override to interface methods.
Language Scope
Save Actions are exclusive to the Java editor. They do not affect XML, JSON, or TypeScript files. For those, you must use separate plugins or external tools like Prettier.
Verification and Testing
To verify the configuration is working as intended:
- Functional Test: Open a Java file, add a random unused import and break the indentation. Press Ctrl+S. The import should vanish and the indentation should snap back immediately.
- Config Test: Open
.settings/org.eclipse.jdt.ui.prefsin a text editor. Verify thatsaveAction.enabled=trueis present. - Build Test: Run a clean build via Maven or Gradle (
mvn clean compile). The build should succeed, confirming that the Save Actions did not introduce syntax errors.
Quick Reference: Configuration Keys
| File | Key | Purpose |
|---|---|---|
org.eclipse.jdt.ui.prefs | saveAction.enabled | Toggles Save Actions for the project. |
org.eclipse.jdt.ui.prefs | saveAction.actions | List of active cleanup IDs. |
org.eclipse.jdt.core.prefs | org.eclipse.jdt.core.formatter_profile | Identifies the active style profile. |
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.