Take the 2-minute tour ×

What is the best way to ensure that Java code follows a strict set of formatting guidelines. It needs to be configurable so that we can enforce our own guidelines (which are mostly based on Suns original guidelines document).

Im sick of having classes that even in different methods have different formatting depending on the developer who altered that particular method.

When I say strict, I mean, absolutely everything is formatted to a standard.

share|improve this question
The best place to enforce this is in your source control system, so it might be worth stating which one you're using so that you can get matching tools there and in your IDE. –  McDowell May 23 '11 at 0:01
add comment

3 Answers

In Eclipse, you can set up your own set of guidelines. Go to Window/Preferences and search for "format", and you'll find Code Formatter somewhere in the menu tree. To apply formatting to a file, just hit Ctrl+Shift+F in the editor.

Then you can share the code formatting profile with the entire team, and everybody will be able to format the code the exact same way.

share|improve this answer
Wow, I haven't looked at this for a while, the formatter seems quite comprehensive. I wonder if it is possible to apply this formatting to all files in a project at once. –  Jacob May 23 '11 at 0:20
yes, it is. right click any directory, and format. –  MeBigFatGuy May 23 '11 at 0:51
you can choose to automatically format on save. See Java/Save Action in preferences. –  MeBigFatGuy May 23 '11 at 0:52
add comment

Some source code management systems can be configured to run a command-line formatter on all code being checked in.

share|improve this answer
add comment

If you want to rely on Eclipse alone, you can configure it in the preferences under Java -> Compiler -> Errors/Warnings to catch basic style problems and you can configure the formatter to format the code to your specification.

From style perspective, Eclipse options are quite limited so it is better to use external tools like Checkstye and Findbugs which can be configured as part of your build process and also work well with Eclipse.

share|improve this answer
Well if Eclipse's styling options are quite limited then I don't know how you'd describe Visual Studio's ;) If you mean catching misformated code - I'd think just autoformatting at checkin or save would be easier than another tool. –  Voo May 23 '11 at 1:19
add comment

Your Answer


