我正在尝试使用 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窗口。
你能帮我实现这个目标吗? 提前致谢
最佳答案
将 fc %4 %5>>%6
替换为 fc %1 %2>>%3
。
filecompare.bat
执行时只知道它自己的参数,而不是您传递给 cmd
的参数。
关于java - 在java中使用args执行批处理文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39661236/