Reply to comment

Java Memorandum - Modifying format (style) of Java code using Eclipse

Coding style is very much personal, and using your favorite style improves the coding productivity.

Say, for example, someone (many Java programmer, apparently) likes the following style.

class test(){
    public
main(args[]){
        if(
a == b){
           
System.out.println("Hello World!");
        }
    }
}

I personally never liked putting curly braces in the same line. I always like the following style.

class test()
{
    public
main(args[])
    {
        if(
a == b)
        {
           
System.out.println("Hello World!");
        }
    }
}

Sometimes, you want to change the coding style in your favorite way, like the above. If you are writing code from scratch, Eclipse has the feature called Formatter in Preferences, Java --> Code Style --> Formatter. There, you can set up your own format (i.e. coding style).

But what if you want change existing file?
There's a way to convert existing Java source code at once.

http://wiyoo.blogspot.com/2007/05/batch-formatting-java-source-code-with...

In case the page's gone, here's the excerpt of the linked page.

1. In Eclipse, open the properties of a Java project.
2. In Code Style > Formatter, check Enable project specific settings.
3. Select and edit a formatter configuration.
4. Click on OK.
5. The code formatter configuration file will then be in your Eclipse workspace in workspace/YourJavaProject/.settings/org.eclipse.jdt.core.prefs.

Then, run the following.

eclipse -application org.eclipse.jdt.core.JavaCodeFormatter -config D:/formatter.prefs D:/tmp/src

This will convert all the .java files under D:/temp/src to your format.

Reply

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <code> <cite> <ul> <ol> <li> <dl> <dt> <dd> <p>
  • Lines and paragraphs break automatically.
  • Link to Amazon products with: [amazon product_id inline|full|thumbnail]. Example: [amazon 1590597559 thumbnail]
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Images can be added to this post.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.