IntelliJ does not terminate Spring Boot applications build with gradle
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When developing Spring Boot applications using IntelliJ IDEA and building with Gradle, developers may encounter an issue where the application does not terminate properly. This can become frustrating, especially during iterative development cycles, where starting and stopping applications frequently is common. In this article, we'll explore the root causes of this issue, dive into the specifics of how it manifests, and suggest solutions.
Understanding the Problem
Typical Manifestation
The most common symptom of this issue is that when you stop the execution of a Spring Boot application from IntelliJ, the console indicates that the process is terminated, but the application is still running in the background. This can lead to port conflicts when you try to restart the application, or it might cause resource exhaustion if multiple instances are left running.
Complications
- Port Binding Issues: When you attempt to rerun your Spring Boot application, you might encounter errors that indicate the server port is already in use.
- Resource Leaks: Lingering processes consume system resources and can slow down your development machine.
- Complex Debugging: Having multiple instances running can make debugging more difficult as logs and breakpoints may not correspond to the code you are currently editing and executing.
Technical Explanation
Spring Boot Execution in IntelliJ
When you start a Spring Boot application in IntelliJ, it typically runs within the `Run/Debug` configuration environment. This environment handles the build and execution lifecycle using various tools and configurations.
- Process Handling: IntelliJ utilizes the Java `Process` API to manage the application's lifecycle. Normally, it should send a termination signal that triggers the shutdown hooks in the Spring Boot application.
The Role of Gradle
Gradle, the build automation tool, compiles and packages the application. The issue often stems from how the assembled application handles its termination processes. When using Gradle, some tasks or configurations can interfere with proper shutdown procedures.
Potential Causes
- Gradle Daemons: By default, Gradle uses daemons to run tasks. These processes can sometimes prevent proper shutdown when the build doesn't terminate cleanly.
- Debug Configurations: Incorrect or incomplete debug configurations can fail to trigger the shutdown hooks in a Spring Boot application.
- IntelliJ Settings: Certain IDE settings, such as incorrect JVM options or shortcuts, may impede the proper termination of applications.
Solutions and Workarounds
Adjust Gradle and IntelliJ Configurations
- Kill Daemon Processes: Periodically, it is useful to terminate Gradle daemons that may have become orphaned or are no longer needed.
- Review Build Scripts: Ensure your `build.gradle` or `settings.gradle` scripts are correctly configured to facilitate process termination.
- Disable Gradle deamon if necessary:
- Modify `application.yml` or `application.properties`:
- Ensure server port and stop operations are correctly configured.
- Use Shutdown Hooks: Make sure your application registers shutdown hooks properly.
- Reconfigure JDK Settings: Ensure the correct SDK is being used in the `Run/Debug` configuration. Sometimes toggling between OpenJDK and Oracle JDK can help resolve signaling issues.
- Correct Shortcut Mapping: Verify the mapping of "Stop" shortcuts in IntelliJ to ensure they send the expected signal.
- Use operating system tools like `lsof` or `netstat` to release the port used by the lingering application processes.
- Parallel Execution: Enable parallel task execution in Gradle for efficient builds.
- Gradle Caching: Utilize Gradle's build cache to minimize rebuilds.
- Memory Management: Adjust the IDE's heap space and make use of "Invalidate Caches" or "Restart" options when required.
- IDE Updates: Regularly update IntelliJ for bug fixes related to process management.
Related reading
- IntelliJ Errorjava error release version 5 not supported
- intellij idea - Error java invalid source release 1.9
- Intellij IDEA complains cannot resolve spring boot properties but they work fine
- Intellij IDEA, format all code in a project
- IntelliJ IDEA generating serialVersionUID
- Intellij Idea Importing Gradle project - getting JAVA_HOME not defined yet
- Intellij IDEA Java classes not auto compiling on save
- IntelliJ IDEA jump from interface to implementing class in Java

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.