一尘不染

Java编程:从Java调用exe并传递参数

java

我正在寻找一种从Java调用exe并传递特定参数的机制。我能怎么做?

Process process = new ProcessBuilder("C:\\PathToExe\\MyExe.exe").start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;

System.out.printf("Output of running %s is:", Arrays.toString(args));

while ((line = br.readLine()) != null) {
  System.out.println(line);
}

先前的代码有效。但是我无法在中传递参数。MyExe.exe接受参数。另一个问题是PathToExe有空格。ProcessBuilder似乎无法正常工作。例如:

C:\\User\\My applications\\MyExe.exe

谢谢。


阅读 1135

收藏
2020-03-06

共1个答案

一尘不染

在构造函数本身中传递参数。

Process process = new ProcessBuilder("C:\\PathToExe\\MyExe.exe","param1","param2").start();
2020-03-06