2016-09-23 91 views
0

我想用java命令运行批处理文件。 我有以下代码在java中执行带有args的批处理文件

Runtime.getRuntime().exec("cmd /c start C:\\temp\\Sept_2016\\22Sept\\filecompare.bat"); 

我filecompare.bat命名如下

fc "C:\temp\Sept_2016\22Sept\MSP_Files\msp.txt" "C:\temp\Sept_2016\22Sept\MIB_Files\mib.txt">>"C:\temp\Sept_2016\22Sept\mismatch.txt" 

这工作按批处理文件预期和输出都存储在txt文件。

现在我不想使用上面的硬编码值我想从Java程序中获取值。所以我写Java代码如下

String file1 = new String("C:\\temp\\Sept_2016\\22Sept\\MSP_Files\\msp.txt"); 
String file2 = new String("C:\\temp\\Sept_2016\\22Sept\\MIB_Files\\mib.txt"); 
String file3 = new String("C:\\temp\\Sept_2016\\22Sept\\mismatch.txt"); 
Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", "start C:\\temp\\Sept_2016\\22Sept\\filecompare.bat", file1, file2, file3}); 

在类似的路线,我修改批处理文件

fc %4 %5>>%6 

但这似乎并不奏效。它只是打开dos窗口。

你能帮我实现吗? 在此先感谢

+2

您只传递3个参数为什么使用'%4','%5'和'%6'? – talex

+0

1)“cmd.exe”, 2)“/ c”, 3)“start C:\\ temp \\ Sept_2016 \\ 22Sept \\ filecompare.bat”, 4) )file1, 5)file2, 6)file3 – Sachin

+0

我相信你的String []的前三个元素应该连接在一起。顺便说一句,为什么你不能串联所有的字符串,只传递一个字符串? – freefall

回答

1

fc %4 %5>>%6替换为fc %1 %2>>%3

filecompare.bat正在执行只知道它自己的参数,而不是你传递给cmd的参数。

+0

嘿塔利克斯,谢谢你。非常感谢。它正在工作。我现在看到的唯一问题是它打开了2个命令提示符。对此有何想法? – Sachin

+0

因为建议从参数中删除'start'。 – talex

+0

删除开始,但它打开单个命令提示符,但它不处理我的命令,它只是打开单个命令提示符 – Sachin