ProcessBuilder在Linux下正常工作,但在Windows下不正常。

huangapple go评论96阅读模式
英文:

ProcessBuilder working in Linux but not Windows

问题

这是一个运行echo命令的简单程序,使用ProcessBuilder,在我的Linux机器上运行良好,但在Windows上运行时会抛出IOException异常。

这是我的程序的简化版本。它将echohello作为ProcessBuilder的参数,然后将输出保存到一个字符串中并打印输出。在Linux上,输出是hello,而在Windows上会捕获IOException

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. public class TestPB {
  5. public static void main(String[] args) throws InterruptedException {
  6. ProcessBuilder pb = new ProcessBuilder("echo", "hello");
  7. try {
  8. Process process = pb.start();
  9. BufferedReader readProcessOutput = new BufferedReader(new InputStreamReader(process.getInputStream()));
  10. String output = "";
  11. String line = "";
  12. while ((line = readProcessOutput.readLine()) != null) {
  13. output += line;
  14. output += System.getProperty("line.separator");
  15. }
  16. process.waitFor();
  17. if (output.length() > 0) {
  18. System.out.println(output.substring(0, output.length() - 1));
  19. } else {
  20. System.out.println("No result");
  21. }
  22. } catch (IOException io) {
  23. System.out.println("IOException thrown");
  24. }
  25. }
  26. }

有谁知道为什么在Windows上不起作用吗?

英文:

I have a simple program that runs the echo command with ProcessBuilder, and the program works perfectly fine on my Linux machine, but it throws an IOException when running on Windows.

This is a simplified version of my program. It takes echo and hello as arguments for ProcessBuilder, and then saves the output into a string and prints the output. In Linux, the output is hello, and in Windows an IOException is caught.

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. public class TestPB {
  5. public static void main(String[] args) throws InterruptedException {
  6. ProcessBuilder pb = new ProcessBuilder("echo", "hello");
  7. try {
  8. Process process = pb.start();
  9. BufferedReader readProcessOutput = new BufferedReader(new InputStreamReader(process.getInputStream()));
  10. String output = "";
  11. String line = "";
  12. while ( (line = readProcessOutput.readLine()) != null) {
  13. output += line;
  14. output += System.getProperty("line.separator");
  15. }
  16. process.waitFor();
  17. if(output.length() > 0) {
  18. System.out.println(output.substring(0, output.length() -1));
  19. } else {
  20. System.out.println("No result");
  21. }
  22. } catch (IOException io) {
  23. System.out.println("IOException thrown");
  24. }
  25. }
  26. }

Does anyone know why this is not working in Windows?

答案1

得分: 3

echo在Windows上不是一个程序,而是一个内部的shell命令<sup>*</sup>,因此您需要调用命令行解释器:

  1. ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c", "echo", "hello");

<sup>*) 参考:维基百科</sup>

英文:

echo is not a program on Windows, it's an internal shell command<sup>*</sup>, so you need to invoke the command-line interpreter:

  1. ProcessBuilder pb = new ProcessBuilder(&quot;cmd.exe&quot;, &quot;/c&quot;, &quot;echo&quot;, &quot;hello&quot;);

<sup>*) Reference: Wikipedia</sup>

huangapple
  • 本文由 发表于 2020年10月12日 06:37:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64309665.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定