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 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

Spring Boot provides four 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. Moreover, it’s even possible for exceptions to implement the ExitCodeGenerator interface.

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:

@SpringBootApplication
public class ExitCodeGeneratorDemoApplication implements ExitCodeGenerator {

    public static void main(String[] args) {
        System.exit(SpringApplication
          .exit(SpringApplication.run(DemoApplication.class, args)));
    }
 
    @Override
    public int getExitCode() {
        return 42;
    }
}

Here, the ExitCodeGeneratorDemoApplication class implements the ExitCodeGenerator interface. Also, we wrapped the call to SpringApplication.run() 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:

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

@Bean
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):

@Bean
DemoListener demoListenerBean() {
    return new DemoListener();
}

private static class DemoListener {
    @EventListener
    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.

3.4. Exception with Exit Code

An exception can also implement the ExitCodeGenerator interface. When throwing such exceptions, Spring Boot returns the exit code provided by the implemented getExitCode() method. For instance:

public class FailedToStartException extends RuntimeException implements ExitCodeGenerator {

    @Override
    public int getExitCode() {
        return 42;
    }
}

If we throw an instance of FailedToStartException, Spring Boot will detect this exception as an ExitCodeGenerator and report 42 as the exit code.

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.