如何获得命令行及工艺过程处理的参数在Java中9
的Java 9 provied漂亮的方式来获得Process
的信息,但我还是不知道怎么弄的过程中CommandLine
& arguments
:如何获得命令行及工艺过程处理的参数在Java中9
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
尝试使用ProcessBuilder
代替Runtime#exec()
Process p = new ProcessBuilder("notepad.exe", "E:\\test.txt").start();
或者另一种方式来创建一个进程:
Process p = Runtime.getRuntime().exec(new String[] {"notepad.exe", "E:\\test.txt"});
似乎对此进行了报道中JDK-8176725。以下是描述问题的注释:
命令行参数不可通过非特权API用于其他进程 ,因此可选项始终为空。该API明确表示这些值是操作系统特定的。 如果将来可以通过Window API获取参数,则可以更新实现。
顺便说一句,信息结构是由本机代码填充;这些字段的分配不会出现在Java代码中。
是的,似乎这在Linux,MacOS上工作,但现在不在Window中。 – Jerry06
不太清楚该错误的解决方法,但它看起来像被标记为* Future Project *一样。 – nullpointer
只是为了进一步调试,是否可以尝试用ProcessHandle.current()替换ProcessHandler的'p.toHandle()'并执行以查看,如果在预期的字段中获得了一些值?大部分的兴趣是看你的当前进程的进程处理器是否合适。 – nullpointer
不幸运,仍然是一样的。 – Jerry06