3

I have written a Spring Boot + Spring Batch application in IntelliJ that fails on startup if it faces any configuration issues, which is expected. But the exit code of the application shown in IntelliJ is still 0. Doesn't a 0 error code imply success? What should I do to have the application return the correct exit code?

asked Jan 29, 2018 at 0:15
3
  • That is expected. Commented Jan 29, 2018 at 0:39
  • 1
    Can you please elaborate? Commented Jan 29, 2018 at 0:53
  • Please add the code used for 'failing' startup. If you have used System.exit() then it is using the default. Change it as per your need. IntelliJ would not be customizing the exit code, as it really does not know the app logic Commented Jan 29, 2018 at 5:27

3 Answers 3

1

This is surprisingly awkward to do! SpringApplication.run() has a very bizzare startup procedure, that involves spawning a new thread to do the work and yada yada. In the process of doing this it calls SilentExitExceptionHandler.setup(thread) which has the side effect of ensuring any exit code is always 0!

If you want to work around this you can either splat the uncaught exception handler.

You can also do this:

try {
 SpringApplication.run(...);
}
catch(RuntimeException ex) {
 throw new RuntimeException(ex);
}

By wrapping the exception, spring doesn't recognise it as a special exception and so will exit with an error code of 1. However there are side effects - notably it will log the exception to stderr, but it will also ignore any ExitCodeGenerator beans you have setup.

answered Nov 5, 2021 at 15:09
Sign up to request clarification or add additional context in comments.

Comments

0

If you've programmed it to exit, it might still be 0. You can customize it by building a ExitCodeGenerator.

http://www.logicbig.com/tutorials/spring-framework/spring-boot/app-exit-code/

answered Jan 29, 2018 at 0:57

Comments

0

After a lot of digging I've found that adding the spring-boot-devtools means that Spring will exit with 0 if it's misconfigured. See here: https://github.com/spring-projects/spring-boot/issues/28541

answered Nov 5, 2021 at 15:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.