Java如何调用带参数的exe文件的cmd命令

paodan 2009-09-07 10:27:00
我希望在java中调用外部的exe文件,该文件在doc下执行需要参数,例如exe文件为a.exe;存在D盘下,带三个参数,在doc下执行如下
D:\>a.exe inputfiel -p outputfile
inputfile表示输入的文件,-p是一个属性,outputfile表示输出的文件,我想在java中如何执行,知道用RunTime.exec(cmd);,但是这个cmd怎么写?
...全文
786 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
paodan 2009-09-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 java2000_net 的回复:]
Java codeRuntime.exec("d:/a.exe inputfiel -p outputfile");

就这么写就可以
[/Quote]老紫竹,能否帮我看一下这个问题http://topic.csdn.net/u/20090813/22/74740ed8-e446-4fad-b4c4-d93c1ad539de.html
paodan 2009-09-07
  • 打赏
  • 举报
回复
搞定了,使用老紫竹提供的第二种方法,参数之间有空格使用数组

try {

String commons[] = {"D:\\a.exe" ,"D:\\data\\aa.doc","-p","D:\\data\\bb.doc"};
Process child = Runtime.getRuntime().exec(commons);
BufferedReader buf = new BufferedReader(new InputStreamReader(child.getErrorStream()));
String str = "";
while((str = buf.readLine())!= null)
{
System.out.println(str);
}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
justinavril 2009-09-07
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 paodan 的回复:]
引用 5 楼 justinavril 的回复:
那个inputfile和outputfile是你的文件路径 是参数。。。
是的,比如inputfile = "D:\\data\\a.doc",outputfile = "D:\\data\\b.doc"
Runtime.exec("d:/a.exe " + inputfiel + " -p " + outputfile);
getErrorStream得到的是 Couldn't open file 'D:\\data\\a.doc'
[/Quote]
你这个a.exe具体什么功能?
paodan 2009-09-07
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 justinavril 的回复:]
那个inputfile和outputfile是你的文件路径 是参数。。。
[/Quote]是的,比如inputfile = "D:\\data\\a.doc",outputfile = "D:\\data\\b.doc"
Runtime.exec("d:/a.exe " + inputfiel + " -p " + outputfile);
getErrorStream得到的是 Couldn't open file 'D:\\data\\a.doc'
justinavril 2009-09-07
  • 打赏
  • 举报
回复
那个inputfile和outputfile是你的文件路径 是参数。。。
paodan 2009-09-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 java2000_net 的回复:]
Java codeRuntime.exec("d:/a.exe inputfiel -p outputfile");

就这么写就可以
[/Quote]我用getErrorStream得到的是 Couldn't open file 'inputfile'
mlhm2 2009-09-07
  • 打赏
  • 举报
回复
学习了,thanks
老紫竹 2009-09-07
  • 打赏
  • 举报
回复
老紫竹 2009-09-07
  • 打赏
  • 举报
回复
Runtime.exec("d:/a.exe inputfiel -p outputfile");


就这么写就可以

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧