如何在Java 9中获取Process的commandLine和参数

Vie*_*iet 9 java windows process java-9

Java 9提供了获取信息的漂亮方式Process,但我仍然不知道如何获取进程的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(""));
Run Code Online (Sandbox Code Playgroud)

结果:

Arguments : 0
Command : C:\Windows\System32\notepad.exe
CommandLine : 
Run Code Online (Sandbox Code Playgroud)

但我期待:

Arguments : 1
Command : C:\Windows\System32\notepad.exe
CommandLine : C:\Windows\System32\notepad.exe E:\\test.txt
Run Code Online (Sandbox Code Playgroud)

man*_*uti 9

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

对于其他进程,命令行参数不能通过非特权API使用,因此Optional始终为空.API明确指出值是特定于操作系统的.如果将来可以通过Window API获得参数,则可以更新实现.

顺便说一句,信息结构由本机代码填充; 字段的分配不会出现在Java代码中.


Gil*_*ili 5

JDK-8176725表示 Windows 尚未实现此功能。这是一个简单但缓慢的解决方法:

  /**
   * Returns the full command-line of the process.
   * <p>
   * This is a workaround for
   * <a href="/sf/answers/3273763251/">/sf/answers/3273763251/</a>
   *
   * @param processHandle a process handle
   * @return the command-line of the process
   * @throws UncheckedIOException if an I/O error occurs
   */
  private Optional<String> getCommandLine(ProcessHandle processHandle) throws UncheckedIOException {
    if (!isWindows) {
      return processHandle.info().commandLine();
    }
    long desiredProcessid = processHandle.pid();
    try {
      Process process = new ProcessBuilder("wmic", "process", "where", "ProcessID=" + desiredProcessid, "get",
        "commandline", "/format:list").
        redirectErrorStream(true).
        start();
      try (InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream());
           BufferedReader reader = new BufferedReader(inputStreamReader)) {
        while (true) {
          String line = reader.readLine();
          if (line == null) {
            return Optional.empty();
          }
          if (!line.startsWith("CommandLine=")) {
            continue;
          }
          return Optional.of(line.substring("CommandLine=".length()));
        }
      }
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  }
Run Code Online (Sandbox Code Playgroud)


Myk*_*nko 1

尝试使用ProcessBuilder而不是Runtime#exec()

Process p = new ProcessBuilder("notepad.exe", "E:\\test.txt").start();
Run Code Online (Sandbox Code Playgroud)

或者另一种创建流程的方法:

Process p = Runtime.getRuntime().exec(new String[] {"notepad.exe", "E:\\test.txt"});
Run Code Online (Sandbox Code Playgroud)