我有一个用 C 编写的 Windows 程序(比方说,一个 launcher
)和一个打包在 *jar
中的基于 javaFX 的应用程序( 有效载荷
)。我想要做的是实现类似于 JetBrains 的 IntelliJ IDEA 行为方式的东西。我的意思是,在任务管理器中我们可以看到这样的进程“树”或“文件夹”:
但是在我的例子中,我看到了两个完全独立的进程:launcher.exe
(仅在 [Details]
部分)和一个 Java (TM)
(实际上是我的[Processes]
部分中的 Platform SE 二进制文件payload
)。
我正在使用 CreateProcessW
函数来生成我的进程。在我的代码中:
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);
如何使我的 Java 应用程序在任务管理器中看起来更“原生”?我正在使用 jdk1.8.0_172
。感谢您的帮助。
最佳答案
要创建在任务管理器中显示的进程“树”或“文件夹”,您需要满足以下两个要求:
- 树中的进程正在执行相同的程序/应用程序(按应用程序分组)。
- 这些进程之间存在父子关系。有一个父进程启动其他进程,这些进程是子进程。 (要检查父子关系,您可以使用 Process Explorer 工具。)
对于您的情况,虽然 launcher.exe 和 java.exe (payload.jar) 具有父子关系但不是同一个程序/应用程序,因此它们不能位于进程“树”或“文件夹”中' 在任务管理器中。
引用“About Processes and Threads”“Child Processes”
为了演示如何创建进程“树”,我创建了以下演示:一个 win32 控制台应用程序。 (启动 TestGroupProcesses.exe,每次按 Enter 都会在进程树中创建一个子进程。)
#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);
}
上面演示创建的进程“树”如下所示:
关于java - 如何在 Windows 中运行子 java 进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56597759/