How to auto align “{” braces of statement blocks for Java in Eclipse

Eclipse 3.4 for Java EE came with a default code style for Java where the braces of statement blocks are not aligned. It looks like this: 

public class MyClass
    private String myVar; 

    public MyClass() {
       myVar = "Some text here for now";

Instead, some people prefer to align statement block braces because it’s easier to keep track of them that way. Aligned braces look like this:

public class MyClass
    private String myVar; 

    public MyIntStack()
	 myVar = "Some text here for now";

Here’s how to change this so that any auto-generated Eclipse code (like Source > Generate Getters and Setters) shows up with aligned braces.

Step 1. Go to Eclipse Preferences.

  • On OS X go to Eclipse > Preferences.
  • Click on arrow next to Java in the list on the left
  • In the expanded menu, click on arrow next to Code Style
  • Click on Formatter
  • Click the Edit… button ( In EasyEclipse 1.2, it’s the Show… button)


Step 2. Create a custom Profile.

  • Select the Braces tab
  • Change the “Profile name” to something unique
  • And now a somewhat tedious task: Select “Next line” for each of the elements under “Brace positions” (wouldn’t it be great if there was a “Change All” drop-down option? Mmmm, Usability?)


How to set up Connector/J in Eclipse

Connector/J is a “Type IV JDBC driver for MySQL.”


You have downloaded & installed MySQL 5.1, Connector/J for 5.1 (the JDBC driver for MySQL) from, Tomcat 5.5 or 6, Eclipse 3.4 for Java EE. I’m assuming this is a Dynamic Web Project in Eclipse with Tomcat 6 as the Target Runtime. I did this successfully on OS X 10.5 (Leopard) & on Windows XP.

The Steps:

  1. Select your project folder in Eclipse’s Project Explorer (just click on it so it’s highlighted).
  2. Open project properties. There’re several ways to do this. For example, you can go to Project > Properties in Eclipse’s top menu.
  3. Click on Java Build Path. Click “Add External Jars.” Navigate to where you unzipped Connector/J on your hard drive. Choose the .jar file (in my case mysql-connector-java-5.1.7-bin.jar) & click Open. It’ll now show up under JARs and class folders in the build path.
  4. Click on Java EE Module dependencies in the menu on the left.
  5. Check the checkbox next to the Connector/J path under JAR/Module. Hit Apply and OK.
    Run a skeleton test Servlet to see if it works:

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestJDBC extends HttpServlet {

    public void init(){
        try {
            System.out.println("JDBC driver loaded");
        } catch (ClassNotFoundException e) {
        } //Eclipse may auto-force some extra catch blocks here

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("Check the Console tab in" +
		" Eclipse... it should say \"JDBC driver loaded\"");