Home » Java » Enterprise Java » Spring Boot Exit Codes – Create Custom Exit Code

About Shubhra Srivastava

Shubhra Srivastava
Shubhra is a software professional and founder of ProgrammerGirl. She has a great experience with Java/J2EE technologies and frameworks. She loves the amalgam of programming and coffee :)

Spring Boot Exit Codes – Create Custom Exit Code

When running a Spring Boot application, we get a system exit code of 0, when everything goes fine.  For any unhandled exceptions, the application returns with an exit code 1.

It’s possible for us to return a custom exit code from our Spring Boot application. In this tutorial, we’ll learn to do so.

Implementing ExitCodeGenerator:

Let’s start by creating a class that implements the ExitCodeGenerator interface:

public class SampleApplication implements ExitCodeGenerator {
    public static void main(String[] args) {
          .exit(SpringApplication.run(SampleApplication.class, args)));
    public int getExitCode() {
        return 25;

We have overridden the getExitCode() method to return a value of 25. So, this application will now exit with an exit code of 25.

We have wrapped the SpringApplication.run() with the SpringApplication.exit() method.

As per the Spring documentation, we must call System.exit() with the result of the call to the SpringApplication.exit() method.

Listening to ExitCodeEvent:

We can register an event listener to listen to an ExitCodeEvent:

SampleEventListener sampleEventListener() {
    return new SampleEventListener();
private static class SampleEventListener {
    public void exitEvent(ExitCodeEvent event) {
        LOG.info("Application Exit code: {}", event.getExitCode());

Spring Boot will fire this event when it finds an application-specific exit code. Now on an application exit, exitEvent() method will be invoked.

Working with ExitCodeExceptionMapper:

ExitCodeExceptionMapper is a strategy interface that we can use to provide mappings between exception types and exit codes.

ExitCodeExceptionMapper exitCodeToexceptionMapper() {
    return exception -> {
        if (exception.getCause() instanceof NumberFormatException) {
            return 34;
        if (exception.getCause() instanceof CustomTypeException) {
            return 45;
        return 1;

Now, for an exception of type NumberFormatException, our application will exit with an exit code of 34 and so on.


In this quick tutorial, we learned how to return a custom exit code in a Spring Boot application. We also implemented an event listener for the ExitCodeEvent.

Returning a correct exit code will help us while troubleshooting an application.

Published on Java Code Geeks with permission by Shubhra Srivastava, partner at our JCG program. See the original article here: Spring Boot Exit Codes – Create Custom Exit Code

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments