调用可执行文件并传递参数 [英] Call an executable and pass parameters

查看:169
本文介绍了调用可执行文件并传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一种从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();

这篇关于调用可执行文件并传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆