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?
-
That is expected.reflexdemon– reflexdemon2018年01月29日 00:39:48 +00:00Commented Jan 29, 2018 at 0:39
-
1Can you please elaborate?lebowski– lebowski2018年01月29日 00:53:45 +00:00Commented 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 logicgargkshitiz– gargkshitiz2018年01月29日 05:27:59 +00:00Commented Jan 29, 2018 at 5:27
3 Answers 3
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.
Comments
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/
Comments
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
Comments
Explore related questions
See similar questions with these tags.