eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

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.

Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – LS – NPI (cat=Java)
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

eBook Jackson – NPI EA – 3 (cat = Jackson)