本文介绍了Java编程:从Java调用exe并传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在找出一种从Java调用exe并传入特定参数的机制。我该怎么办?
I'm figuring out a mechanism to call an exe from Java and passing in specific parameters. How can I do?
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似乎无法正常工作。例如:
The previous code works. But I'm not able to pass parameters in. MyExe.exe accepts parameters. An other problem is when PathToExe has blank spaces. ProcessBuilder seems not working. For example:
C:\\User\\My applications\\MyExe.exe
谢谢。
推荐答案
在构造函数本身传递参数。
Pass your arguments in constructor itself.
Process process = new ProcessBuilder("C:\\PathToExe\\MyExe.exe","param1","param2").start();
这篇关于Java编程:从Java调用exe并传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!