1. Overview

Despite the ability to format code within an Integrated Development Environment (IDE), we might want to use command-line formatters to automate the process of formatting code. Therefore, we can ensure the usage of the same code style even if there are many developers and the codebase is large.

In this tutorial, we’ll discuss formatting Java code from the command line. We’ll run the examples in Linux, but the formatters we’ll discuss are also available in other operating systems like Windows.

2. Sample Code

We’ll use an unformatted version of the simple “Hello World” program:

    public class HelloWorld 
{
        public static     void main(   String[]    args   )
    {
System.out.println(   
    "Hello World!")
 ;
    } }

There are several problems with the format of this code:

  • The code isn’t indented properly
  • There are superfluous whitespaces such as static     void
  • The expression starting with System.out.println spans more than one line
  • The closing curly braces of the class and the method are on the same line

Additionally, we prefer a starting curly brace to be attached to the end of a line that starts a class or method, not to be on a new line.

First, let’s test whether we can compile and run the unformatted code:

$ javac HelloWorld.java
$ java HelloWorld
Hello World!

The program compiles and runs as expected.

3. Using astyle

astyle (Artistic Style) is a source code formatter that supports several languages, including Java. The version of astyle we’ll use is 3.6.3. Once we download it, we can use it to format HelloWorld.java:

$ astyle --squeeze-ws --style=java HelloWorld.java
Formatted  /home/baeldung/projects/formatter/HelloWorld.java

The –squeeze-ws option removes superfluous whitespaces. The –style=java option specifies using attached braces. Finally, we passed the input file, HelloWorld.java. We can pass multiple files. It can also process directories recursively.

Let’s check the content of the formatted HelloWorld.java:

$ cat HelloWorld.java
public class HelloWorld {
    public static void main( String[] args ) {
        System.out.println(
            "Hello World!")
        ;
    }
}

Now, the source code is formatted with proper indentation. astyle uses a 4-space indentation by default. There are no superfluous spaces. Each closing curly brace is on a new line.

However, the expression starting with System.out.println still occupies three lines.

astyle provides many more options that can customize the formatting of source code both in Java and other languages.

4. Using google-java-format

google-java-format is another option for formatting Java code from the command line. It formats source code using Google Java Style. We can also use it as a plugin in IDEs like IntelliJ and Eclipse.

The version of google-java-format we’ll use is 1.24.0. Once we download the corresponding jar file, we can run it using java:

$ java -jar ./google-java-format-1.24.0-all-deps.jar -r HelloWorld.java

The -r option specifies replacing the input file with the formatted version. Otherwise, google-java-format sends the output to stdout. We passed the input file to be formatted, HelloWorld.java, after the -r option. It’s possible to format more than one file. It can also process files in directories.

Let’s check the content of the formatted HelloWorld.java:

$ cat HelloWorld.java
public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello World!");
  }
}

As is apparent from the output, google-java-format formatted the source code properly and eliminated all the formatting problems we listed before. Google Java Style uses a 2-space indentation by default.

google-java-format has many other options. We can list them using the -h option.

5. Using idea.sh format

IntelliJ is a popular IDE for developing Java applications. Normally, we use the shell script provided by IntelliJ, idea.sh, to launch the IDE. However, we can also format source code from the command line when we run it together with the format keyword, i.e., idea.sh format:

$ idea.sh format -allowDefaults HelloWorld.java
...
Formatting /home/baeldung/projects/formatter/HelloWorld.java...OK

1 file(s) scanned.
1 file(s) formatted.

We’ve truncated the output since it’s long. The command-line formatter launches an instance of the IntelliJ IDE and formats the source code. Therefore, it fails if we have another instance of IntelliJ running.

The -allowDefaults option uses the default code style settings. We passed the file to be formatted, HelloWorld.java, after this option. It’s possible to format multiple files and files in directories.

Let’s check the content of the formatted HelloWorld.java:

$ cat HelloWorld.java 
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println(
                "Hello World!")
        ;
    }
}

All the formatting problems seem to have gone except the statement starting with System.out.println. It still occupies three lines.

Using the -allowDefaults option is helpful when there’s no code style defined or the file doesn’t belong to a project. However, it’s also possible to specify other code style settings using the -s option. We can use the code style settings in the project directory in this case.

If we use another code style setting using the -s option, we can turn off keeping line breaks by setting the ij_java_keep_line_breaks option to false in the code style configuration file:

ij_java_keep_line_breaks = false

Besides the -allowDefaults and -s options, idea.sh format supports other options as well.

6. Using Eclipse’s Formatter Application

Eclipse is another popular IDE for developing Java applications. Just like IntelliJ, Eclipse also supports formatting source code from the command line:

$ eclipse -noSplash -data /home/baeldung/eclipse-workspace -application org.eclipse.jdt.core.JavaCodeFormatter -config org.eclipse.jdt.core.prefs HelloWorld.java
...
Configuration Name: org.eclipse.jdt.core.prefs
Starting format job ...
Done.

We’ve truncated the output as it’s long. The -noSplash option is for disabling the splash screen. The formatter requires a workspace, so we pass the workspace directory using the -data option. It’s /home/baeldung/eclipse-workspace in our example.

We specify to run the formatter using the -application org.eclipse.jdt.core.JavaCodeFormatter option. Running the formatter fails if we have another instance of Eclipse running.

The last option is the -config option. We use the -config option to specify the configuration file for the formatter application. This file can be created from the code formatter settings of a Java project within the Eclipse IDE. Another alternative is to copy and use an existing configuration file.

We used the org.eclipse.jdt.core.prefs configuration file in our example. We set the org.eclipse.jdt.core.formatter.tabulation.char parameter in the configuration file to space to use spaces instead of tabs for indentation:

org.eclipse.jdt.core.formatter.tabulation.char=space

Finally, we passed the file to be formatted, HelloWorld.java. We can specify multiple source files or directories.

Let’s check the content of the formatted HelloWorld.java:

$ cat HelloWorld.java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

As is apparent from the output, Eclipse’s formatter application formatted the source code properly and eliminated all the formatting problems we listed before.

7. Conclusion

In this article, we discussed formatting Java code from the command line in Linux. We used an unformatted version of the “Hello World” program in the examples.

Firstly, we examined astyle. It not only supports Java but also other languages like C and C++.

Secondly, we examined google-java-format, which formats Java code to comply with Google Java Style. It can be used as a command-line program and a plugin in several IDEs.

Then, we saw that the two popular IDEs, IntelliJ and Eclipse, also provide command-line tools to format Java code. We learned that we needed to execute the idea.sh format command to run the IntelliJ’s formatter. Similarly, we used the org.eclipse.jdt.core.JavaCodeFormatter application together with eclipse to run Eclipse’s code formatter from the command line.