Sachin Sachin - 7 months ago 35
Java Question

Execute batch file with args in java

I am trying to run batch file using java command.
I have following code

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

I have batch file named filecompare.bat as below

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"

This works as per expected and output gets stored in txt file.

Now I don't want to use hard coded values as above I want to get the values from Java program. So I am writing java code as below

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});

On the similar lines I am modifying batch file as

fc %4 %5>>%6

But this does not seem to be working. It is just opening dos window.

Could you please help me achieve this?
Thanks in advance


Replace fc %4 %5>>%6 with fc %1 %2>>%3.

filecompare.bat being executed only aware of its own parameter, not parameters you pass to cmd.