Java 9提供了一种很好的方式来获取Process
的信息,但我仍然不知道如何获取进程的CommandLine
和arguments
:
Java 9提供了一种漂亮的方法来获取进程的信息,但我还不知道如何获取进程的CommandLine
和arguments
。
Process p = Runtime.getRuntime().exec("notepad.exe E:\\test.txt");
ProcessHandle.Info info = p.toHandle().info();
String[] arguments = info.arguments().orElse(new String[]{});
System.out.println("Arguments : " + arguments.length);
System.out.println("Command : " + info.command().orElse(""));
System.out.println("CommandLine : " + info.commandLine().orElse(""));
结果:
Arguments : 0
Command : C:\Windows\System32\notepad.exe
CommandLine :
但是我期望:
Arguments : 1
Command : C:\Windows\System32\notepad.exe
CommandLine : C:\Windows\System32\notepad.exe E:\\test.txt
p.toHandle()
替换为ProcessHandle.current()
并执行,以查看是否在预期字段中获得一些值。主要是为了查看当前进程的处理程序是否合适。 - Naman