When a Java application restarts upon closing an RDP session on Windows Server 2022, it is typically because the process is launched via a user profile startup script or scheduled task that lacks proper session isolation. To resolve this, you must configure the application to run as a persistent service rather than a foreground process tied to the interactive desktop.
Understanding the Root Cause
The behavior described occurs because the Java application is likely configured to start automatically when any user logs into the server. In Windows Server environments, applications placed in theStartup folder or registered in the Run registry keys for all users will execute whenever a new session begins. When you close the RDP window, the session ends, but if the application was started by a trigger that persists beyond the session (such as a service or a misconfigured scheduled task), it may appear to restart or behave erratically.
This issue is distinct from standard client-side disconnections. On Windows Server 2022, the Remote Desktop Services (RDS) architecture handles multiple sessions differently than consumer versions of Windows. If your Java application relies on the interactive desktop environment, closing the session terminates the associated processes. However, if the application is set to restart due to a dependency or a watchdog script, it will relaunch immediately. This often happens when administrators use Group Policy Objects (GPOs) to enforce application deployment without considering session state management.
For more information on securing your RDP connections against unauthorized access during these transitions, see How to Restrict RDP and VPN Users/Connections on Windows Server 2022.
Solution 1: Run as a Windows Service
The most robust solution is to decouple the Java application from the user session entirely by running it as a Windows Service. Services run in Session 0, which is isolated from interactive user sessions. This ensures the application continues to operate regardless of whether any user is logged in via RDP. To implement this, you should use a wrapper tool such asWinSW (Windows Service Wrapper) or NSSM (Non-Sucking Service Manager). These tools allow you to register a standard executable or Java JAR file as a native Windows Service.
1. Download the chosen wrapper tool and place it in a secure directory, such as C:\Program Files\JavaService.
2. Create a configuration file (e.g., service.xml) that defines the Java executable path, classpath, and JVM arguments.
3. Install the service using the command line: nssm install MyJavaApp.
4. Set the startup type to "Automatic" in the Services console (services.msc).
5. Restart the service to ensure it launches independently of user logins.
By moving the application to Session 0, closing your RDP session will no longer affect its lifecycle. The application will remain active in the background, providing consistent availability for other clients or internal processes.
Solution 2: Adjust Scheduled Task Settings
If running as a service is not feasible due to specific UI requirements, you can adjust the Scheduled Task settings to prevent interference with RDP sessions. Many administrators use Scheduled Tasks to launch applications at startup, but default settings often tie them to the "Log on User" context. Open the Task Scheduler and locate the task responsible for launching the Java application. Right-click the task and select Properties. Navigate to the General tab and ensure that "Run whether user is logged on or not" is selected. Then, go to the Conditions tab and uncheck "Start the task only if the computer is on AC power" if applicable, though this is less relevant for servers. Crucially, check the Settings tab. Ensure that "If the task fails, restart every" is set appropriately, but verify that the task does not have a condition requiring an interactive session. You may also need to modify the "Run as" account to use a dedicated service account rather than the interactive user account. This prevents the task from being terminated when the specific user's RDP session closes.Solution 3: Configure Group Policy for Session Disconnect
Windows Server 2022 allows administrators to define what happens when an RDP session is disconnected versus closed. By default, some configurations may terminate applications upon disconnect. You can change this behavior via Group Policy. Navigate toComputer Configuration > Policies > Administrative Templates > Windows Components > Remote Desktop Services > Remote Desktop Session Host > Session Time Limits. Here, you can configure policies that keep sessions alive even when disconnected. Additionally, check Remote Desktop Session Host > Connections for settings related to "Restrict remote desktop services users to a single Remote Desktop Services session." Ensuring that sessions are preserved rather than killed can help maintain application state.
While this approach helps preserve the session itself, it does not guarantee the application won't restart if the session is explicitly ended. Therefore, combining this with the Service or Scheduled Task methods provides the best stability. For broader context on managing RDP connectivity issues, refer to RDP Won't Connect with Public IP Despite Local Network Port Being Open.