Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In Eclipse’s Java editor, press Ctrl+Shift+F or choose Source > Format. With code selected, Eclipse formats that selection; with no selection, it formats the entire source file. To make formatting consistent, choose the active profile in Preferences > Java > Code Style > Formatter.

Format a selection or an entire Java file

  1. Open the Java source file in Eclipse’s Java editor.

  2. Select the code you want to format, or leave the editor with no selection to format the whole file.

  3. Press Ctrl+Shift+F, or choose Source > Format. Eclipse applies the active formatter profile to the chosen scope.

    Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The Eclipse JDT documentation describes this selection-versus-file behavior in its Formatter instructions. The available source commands are also listed under Source Actions.

Choose or customize a formatter profile

To change the formatting rules, open Preferences > Java > Code Style > Formatter. Select a built-in profile, or choose New… to create one based on a built-in or existing profile. Select a profile and choose Edit… to adjust its settings. The page also supports importing and exporting formatter profiles.

These settings are documented in Eclipse’s Code Formatter Preferences. Because the help pages use the rolling latest documentation, labels or defaults may differ in other Eclipse releases.

Share a formatter setting with a project

A workspace preference affects your Eclipse workspace. If a Java project needs its own shared formatting rules, open Project > Properties > Java Code Style > Formatter and enable project-specific settings. Eclipse stores project preferences in the project’s .settings directory, so they can be shared with the project.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Eclipse’s JDT tips and guide to generating a formatter application config file describe the project configuration. For the formatter preference specifically, the documented file to copy is .settings/org.eclipse.jdt.core.prefs.

Format code automatically when saving

  1. Open Preferences > Java > Editor > Save Actions.

  2. Enable Perform the selected actions on save.

  3. Select Format source code, then choose whether to format all lines or only edited lines.

Save actions use the active formatter profile. Eclipse documents save actions and format-on-save as off by default on its Java Save Actions Preferences page.

Format multiple Java resources

For a batch operation, the JDT source-format action can also be applied to multiple selected Java files, packages, source folders, or Java projects. Select the resources in the relevant view and run Source > Format. The available commands are described in the Eclipse Source Actions documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Formatting is separate from organizing imports

Source > Format controls code layout and related formatting choices. It does not replace Source > Organize Imports, which removes unused imports and arranges required imports according to separate preferences. Run Organize Imports separately when you want import cleanup; see Eclipse’s Organize Imports Preferences.

Use the JDT formatter from a plugin

For plugin or programmatic workflows, JDT exposes the formatter through ToolFactory.createCodeFormatter and related overloads. A caller supplies formatter options, requests formatting for a compilation unit, source region, or another supported source kind, receives a TextEdit, and applies that edit to a document. Comment formatting depends on the relevant options and flags. The implementation details are in Eclipse’s API guide, Using the code formatter.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.