Did this blog help you solve a problem? Tip me with Bitcoins at: 17dQktcAmU4urXz7tGk2sbuiCqykm3WLs6

Wednesday, November 11, 2009

Setup Eclipse for Efficient Java Coding

The default Java coding settings in Eclipse are fine until you realize one day that there are some really neat things that Eclipse can do automatically for you that aren't activated by default. Here's how I like to set up my Eclipse development environment after a clean install.

Step 1: Make the text editor show line numbers. This helps immensely when reading stack traces and locating errors in your code. Go to the Eclipse Preferences, drill down to "Text Editors" and check the box next to "Show line numbers".

Step 2: Make the Workspace build, refresh, and save automatically. Refreshing forces the folders and files shown in the Package Manager View to be updated when anything changes in those directory behind the scenes. Drill down to "Workspace" and check the boxes.

Step 3: Automate certain tasks whenever you save. Drill down to "Save Actions" and make your selections. Formatting the source code automatically is one of the greatest timesavers. How it's formatted will be defined in Step 3. Under additional actions, my favorite save action is "Remove unused imports". Organizing imports is very time-saving as well. Each time you new-up a new class in a java file, just hit save and the import for that class will be automatically generated.

Step 4: Set up the Code Formatter. This properly indents your code, adds blank lines where you want them, etc., all automatically. Drill down to "Formatter" and make your selections. There are too many choices here for me to go through and I actually use settings very close to the default. The important thing is that the Formatter was activated in step 3. Your changes to the formatter can be saved and shared between developers on a team so that all code is formatted consistently. Use the export button to export an XML file.

Step 5: Setup code templates. For example, when a new class is created, you can have it add certain comments formatted in the way you like. Drill down to "Code Templates" and make your selections.

Step 6: Setup custom editor templates. For example, I often need a log4j logging member in a class, and instead of typing it all from hand or copying and pasting from another class, an editor template can be created to speed up the process. Drill down to "Java" --> "Editor" --> "Templates" and import the following XML pasted into an XML file.


Step 7: See all your changes in effect. Create a test class, add some fields with different indentations, new up an ArrayList, and save. You should see line numbers. You should see the fields automatically align and the import for the ArrayList be added to the imports. Add a method with a parameter that returns a String, type /** above the method definition, and hit enter. You should see a JavaDoc comment automatically be created according to how you defined it in the code templates.

Piece of Cake!!


Pedro said...

Enjoy reading your blog

Cheers from Portugal

Tim Molter said...

Hi Pedro, Thanks for stopping by! Cheers from Germany!

Lex said...

love your blog, thanks for all the tips!