gpt4 book ai didi

java - 如何在 Java 9 中获取进程的命令行和参数

转载 作者:可可西里 更新时间:2023-11-01 12:43:16 26 4
gpt4 key购买 nike

Java 9 提供了获取 Process 信息的好方法,但我仍然不知道如何获取 CommandLinearguments过程:

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

最佳答案

这似乎是在 JDK-8176725 中报告的.以下是描述该问题的评论:

The command line arguments are not available via a non-privileged API for other processes and so the Optional is always empty. The API is explicit that the values are OS specific. If in the future, the arguments are available by a Window APIs, the implementation can be updated.

BTW, the info structure is filled by native code; the assignments to the fields do not appear in the Java code.

关于java - 如何在 Java 9 中获取进程的命令行和参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46767418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com