0

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

10
  • 2
    You passing only 3 parameters why you use %4, %5 and %6? Commented Sep 23, 2016 at 12:55
  • I am passing 5 parameters as below 1) "cmd.exe", 2) "/c", 3) "start C:\\temp\\Sept_2016\\22Sept\\filecompare.bat", 4) file1, 5) file2, 6) file3 Commented Sep 23, 2016 at 12:57
  • I belive that first three elements of your String[] should be concatenated. And by the way, why can't you just concatanate all the string and just pass a single String? Commented Sep 23, 2016 at 12:57
  • You have combined two parameters which can cause problems: start and C:\\temp\\Sept_2016\\22Sept\\filecompare.bat. Anyway start is not needed you can remove it. Commented Sep 23, 2016 at 12:58
  • 1
    @Sachin Yes. You passing 5 parameters to cmd but when it invokes filecompare.bat it pass only 3 parameters there. filecompare.bat know nothing about those 5 parameters. It knows only about parameters that was passed to it. Commented Sep 23, 2016 at 13:00

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Hey Talex, thanks man. Thanks a lot. It is working now. The only problem I can see now is that it is opening 2 command prompts. Any thought on that?
As it was suggested remove start from parameters.
Removed start but then it is opening a single command prompt but it is not processing my command, it simply opens single command prompt
Post it as another question.
My problem got solved. Actually there was another pience of code which was opening another command prompt. I removed that code and now it is working as expected. Just one note to all guys. Start is needed to open up command prompt If I remove start then it does not open the command prompt.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.