How could I run a child java process in Windows?

Netherwire :

I have a Windows program written in C (let's say, a launcher) and a javaFX-based application packed in *jar (a payload). What I want to do is to achieve something similar to how JetBrains's IntelliJ IDEA behaves. I mean, in the task manager we can see a process 'tree' or 'folder' like this: Windows Task Manager

However in my case I see two completely independent processes: the launcher.exe (only in the [Details] section) and a Java (TM) Platform SE binary in the [Processes] section (actually it's my payload).

I am using a CreateProcessW function to spawn my process. In my code:

STARTUPINFOW info = { sizeof(info) };
PROCESS_INFORMATION processInfo;

CreateProcessW(L"C:\\Path\\To\\java.exe", L" -jar C:\\Path\\To\\payload.jar",
    NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &info, &processInfo);

How can I make my java application look more "native" in the Task Manager? I am using jdk1.8.0_172. Thanks for help.

Rita Han - MSFT :

To create a process 'tree' or 'folder' like displayed in task manager you need meet the following two requirements:

  1. The processes in a tree are executing the same program/application (grouped by app).
  2. There is parent-child relationship among these processes. There is a parent process launches other processes those are child processes. (To check the parent-child relationship you can use Process Explorer tool.)

For your case, although the launcher.exe and java.exe (payload.jar) have the parent-child relationship but are not the same program/application so they can't be in a process 'tree' or 'folder' in task manager.

Refer to "About Processes and Threads" "Child Processes"

To demonstrate how to create a process 'tree' I create the following demo: a win32 console application. (Launch the TestGroupProcesses.exe, every time you press the Enter it will create a child process in the process tree.)

#include <windows.h>
#include <stdio.h>

int main()
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));

    printf("Press Enter to create the child process\n");

    while (getchar() != '\n');

    // Start the child process. 
    if (!CreateProcess(NULL,   // No module name (use command line)
        GetCommandLine(),        // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi)           // Pointer to PROCESS_INFORMATION structure
        )
    {
        printf("CreateProcess failed (%d).\n", GetLastError());
    }

    // Wait until child process exits.
    WaitForSingleObject(pi.hProcess, INFINITE);

    // Close process and thread handles. 
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
}

The process 'tree' created by above demo look like this:

enter image description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=122192&siteId=1