在java中用args执行批处理文件

时间:2016-09-23 12:50:50

标签: java batch-file runtime.exec

我正在尝试使用java命令运行批处理文件。 我有以下代码

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窗口。

你可以帮助我实现这个目标吗? 提前致谢

1 个答案:

答案 0 :(得分:1)

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

执行

filecompare.bat时只知道自己的参数,而不是传递给cmd的参数。