【发布时间】:2016-09-23 12:50:50
【问题描述】:
我正在尝试使用 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窗口。
你能帮我实现这个吗? 提前致谢
【问题讨论】:
-
您只传递了 3 个参数,为什么要使用
%4、%5和%6? -
我传递 5 个参数如下 1) "cmd.exe", 2) "/c", 3) "start C:\\temp\\Sept_2016\\22Sept\\filecompare.bat ", 4) 文件1, 5) 文件2, 6) 文件3
-
我相信您的 String[] 的前三个元素应该连接起来。顺便说一句,你为什么不能连接所有的字符串并只传递一个字符串?
-
您组合了两个可能导致问题的参数:
start和C:\\temp\\Sept_2016\\22Sept\\filecompare.bat。无论如何,不需要start,您可以将其删除。 -
@Sachin 是的。您将 5 个参数传递给
cmd,但是当它调用filecompare.bat时,它只传递 3 个参数。filecompare.bat对这 5 个参数一无所知。它只知道传递给它的参数。
标签: java batch-file runtime.exec