Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Generic Top

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


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:

public class ExitCodeGeneratorDemoApplication implements ExitCodeGenerator {

    public static void main(String[] args) {
          .exit(SpringApplication.run(DemoApplication.class, args)));
    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:

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.

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 {

    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.

Generic bottom

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

Generic footer banner
Comments are closed on this article!