1

I am running the Java code from Directory A, and there is a myBat.bat file there too. I want to use Java to execute the bat file. The contents of the myBat.bat is : svn update C:\DirectoryB\file.txt
I have already downloaded the Slik SVN Windows command line client. When i double click on the bat file, it svn updates the file correctly. But not when i run my Java code.

Process p = Runtime.getRuntime().exec("cmd /C C:\\DirectoryA\\myBat.bat");

The test fails because it cannot find the file.txt that it was expecting. In order to really test the svn update, i have deleted the svn file in DirectoryB. Double clicking the bat file repopulates file.txt. The test fails with:
The system cannot find the file specified) at java.io.FileInputStream.open

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
stumped
  • 163
  • 1
  • 2
  • 8
  • Does it work if you use the full path to the batch file? – JustinKSU May 18 '11 at 22:30
  • I just added the full path and it doesn't work either. – stumped May 18 '11 at 22:32
  • There's too much that we miss. please explain what doesn't work and how it doesn't work. – MByD May 18 '11 at 22:32
  • What does happen when you run it from your java code? Are you able to run any process successfully? Try replacing the contents with something other than "svn update" – Kal May 18 '11 at 22:35
  • I replace the bat file contents with : copy C:\a.txt C:\DirectoryB and this did not work either when i executed the Java code. Again, if i double click the batch file directly, it works. – stumped May 18 '11 at 22:43
  • What is the output of when you run the Java program? What command are you using to run it? – JustinKSU May 18 '11 at 22:44
  • Hi Justin/Kal, I figured out the problem. After downloading Slik for the svn cmd line client, i did not restart my Eclipse. So, when Eclipse tried to run the svn update command, it did not recognize svn yet. Which explained why the batch file where i had a copy fileA to fileB worked, but not the svn command. Thanks so much. – stumped May 18 '11 at 22:56
  • Not sure how to vote for you guys.. – stumped May 18 '11 at 22:59
  • @Stumped, post that as an answer to this question and accept it. – mark-cs May 18 '11 at 23:32
  • BTW, Beware of `exec(String command)` it's not a command line parser and is pretty dumb, just splitting on spaces - even if they are in filenames and/or quoted. You should prefer `exec(String[] cmdarray)` and its variants in almost every case. – Stephen P May 19 '11 at 00:53

1 Answers1

1

Try it this way, should work if your bat file is correct:

try {
    Process p = Runtime.getRuntime().exec("cmd /c start c:\\DirectoryA\\myBat.bat");
} catch (IOException ex) {
    ...
}

The idea is that .bat files are not considered to be direct executables by the Runtime.

Costis Aivalis
  • 13,680
  • 3
  • 46
  • 47