Skip to content

Jpackage created single executable does not respond CTRL_C_EVENT or CTRL_CLOSE_EVENT #654

@rifatdover

Description

@rifatdover

Describe the bug
Jpackge packed java application does not respond CTRL_C_EVENT or CTRL_CLOSE_EVENT

Desktop (please complete the following information):

Tested over Windows Server 2019 most probably exist over other

MSFT Build of OpenJDK Version:

openjdk 21.0.6 2025-01-21 LTS
OpenJDK Runtime Environment Microsoft-10800203 (build 21.0.6+7-LTS)
OpenJDK 64-Bit Server VM Microsoft-10800203 (build 21.0.6+7-LTS, mixed mode, sharing)

To Reproduce:
Create this java file under MyApp\src\com\example\Main.java

package com.example;

public class Main {
    public static void main(String[] args) {
        System.out.println("Application started.");

        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            System.out.println(">>>>> Shutdown hook triggered.");
            try {
                Thread.sleep(3000); // Simulate cleanup
                System.out.println(">>>>> Cleanup complete.");
            } catch (InterruptedException e) {
                System.out.println(">>>>> Interrupted during cleanup.");
            }
        }));

        System.out.println("Press Enter to exit...");
        try {
            System.in.read();
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("Exiting app...");
        System.exit(0);
    }
}

Use this batch script to build the app under MyApp\build.bat.

@echo off
setlocal

:: ======= CONFIGURATION =======
set MAIN_CLASS=com.example.Main
set APP_NAME=MyApp
set SRC_DIR=src
set OUT_DIR=out
set JPACKED=jpacked
set JAR_NAME=%APP_NAME%.jar
set MANIFEST_FILE=manifest.txt
set PACKAGE_DIR=package
:: =============================

echo [INFO] Cleaning output and package directories...
if exist %OUT_DIR% rmdir /s /q %OUT_DIR%
if exist %PACKAGE_DIR% rmdir /s /q %PACKAGE_DIR%
if exist %JPACKED% rmdir /s /q %JPACKED%
mkdir %OUT_DIR%
mkdir %PACKAGE_DIR%

echo [INFO] Compiling Java sources...
javac -d %OUT_DIR% %SRC_DIR%\com\example\*.java
if %ERRORLEVEL% neq 0 (
    echo [ERROR] Compilation failed.
    exit /b 1
)

echo [INFO] Creating manifest file...
echo Main-Class: %MAIN_CLASS% > %MANIFEST_FILE%

echo [INFO] Creating JAR file...
jar cfm %PACKAGE_DIR%\%JAR_NAME% %MANIFEST_FILE% -C %OUT_DIR% .

if %ERRORLEVEL% neq 0 (
    echo [ERROR] Failed to create JAR.
    exit /b 1
)

echo [INFO] Building native executable using jpackage...
jpackage --input %PACKAGE_DIR% --name %APP_NAME% --main-jar %JAR_NAME% --main-class %MAIN_CLASS% --dest %JPACKED%/dist --win-console --type app-image

if %ERRORLEVEL% neq 0 (
    echo [ERROR] jpackage failed.
    exit /b 1
)

echo [INFO] Build complete. Executable created at:
echo   %JPACKED%\dist\%APP_NAME%-1.0.exe

:: Optional: run the EXE
echo.
set /p RUN_NOW="Run the EXE now? (y/n): "
if /I "%RUN_NOW%"=="y" (
    echo [INFO] Running the EXE...
    "%JPACKED%\dist\%APP_NAME%-1.0.exe"
)

:: Cleanup (optional)
del %MANIFEST_FILE%
echo [INFO] Done.
endlocal

Expected behavior
When application interrupted with Ctrl+C ShutDownHooks should be activated. Which is working with a java command. For example when application started with java -jar package\MyApp.jar and Ctrl+C pressed shutdown hooks are activated. But when you run the app via jpacked\dist\MyApp\MyApp.exe Shutdown Hooks is not activated.

Screenshots, Logs etc
On both runs Ctrl+C used to exit.
Image

Additional context
This problem may also be present on https://github.com/openjdk/jdk

Metadata

Metadata

Assignees

No one assigned

    Labels

    wontfixThis will not be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions