Generic Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:


1. Overview

Every application returns an exit code on exit; this code can be any integer value including negative values.

In this quick tutorial, we're going to find out how we can return exit codes from a Spring Boot application.

2. Spring Boot and Exit Codes

A Spring Boot application will exit with the code 1 if an exception occurs at startup. Otherwise, on a clean exit, it provides 0 as the exit code.

Spring registers shutdown hooks with the JVM to ensure the ApplicationContext closes gracefully on exit. In addition to that, Spring also provides the interface org.springframework.boot.ExitCodeGenerator. This interface can return the specific code when System.exit() is called.

3. Implementing Exit Codes

Boot provides three methods that allow us to work with exit codes.

The ExitCodeGenerator interface and ExitCodeExceptionMapper allow us to specify custom exit codes while the ExitCodeEvent allows us to read the exit code on exit.

3.1. ExitCodeGenerator

Let's create a class that implements the ExitCodeGenerator interface. We have to implement the method getExitCode() which returns an integer value:

public class ExitCodeGeneratorDemoApplication implements ExitCodeGenerator {

    public static void main(String[] args) {
          .exit(, args)));
    public int getExitCode() {
        return 42;

Here, the ExitCodeGeneratorDemoApplication class implements the ExitCodeGenerator interface. Also, we wrapped the call to with SpringApplication.exit().

On exit, the exit code will now be 42.

3.2. ExitCodeExceptionMapper

Now let's find out how we can return an exit code based on a runtime exception. For this, we implement a CommandLineRunner which always throws a NumberFormatException and then register a bean of type ExitCodeExceptionMapper:

CommandLineRunner createException() {
    return args -> Integer.parseInt("test") ;

ExitCodeExceptionMapper exitCodeToexceptionMapper() {
    return exception -> {
        // set exit code based on the exception type
        if (exception.getCause() instanceof NumberFormatException) {
            return 80;
        return 1;

Within the ExitCodeExceptionMapper, we simply map the exception to a certain exit code.

3.3. ExitCodeEvent

Next, we'll capture an ExitCodeEvent to read the exit code of our application. For this, we simply register an event listener which subscribes to ExitCodeEvents (named DemoListener in this example):

DemoListener demoListenerBean() {
    return new DemoListener();

private static class DemoListener {
    public void exitEvent(ExitCodeEvent event) {
        System.out.println("Exit code: " + event.getExitCode());

Now, when the application exits, the method exitEvent() will be invoked and we can read the exit code from the event.

4. Conclusion

In this article, we've gone through multiple options provided by Spring Boot to work with exit codes.

It's very important for any application to return the right error code while exiting. The exit code determines the state of the application when the exit happened. In addition to that, it helps in troubleshooting.

Code samples can be found over on GitHub.

Generic bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

Comments are closed on this article!