Using Java to do a WIndows command line command
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开发者_Python百科 contents of the myBat.bat is : svn update C:\DirectoryB\file.txt
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.openTry 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.
精彩评论