开发者

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

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜