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.

新しいコメントの投稿

このフィールドの内容は非公開にされ、公表されることはありません。
  • ウェブページアドレスとメールアドレスは、自動的にハイパーリンクに変換されます。
  • 使用できるHTMLタグ: <a> <em> <strong> <code> <cite> <ul> <ol> <li> <dl> <dt> <dd> <p>
  • 行と段落は自動的に折り返されます。
  • Amazon製品へのリンクを次の形式で作成することが出来ます。[amazon product_id inline|full|thumbnail]. 例: [amazon 1590597559 thumbnail]
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • 画像を追加することが出来ます。

書式オプションに関するより詳しい情報...

認証コード
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
画像で表示されている数字および記号を入力してください。