Using Java to do a WIndows command line command

孤人 提交于 2019-12-02 11:07:45

问题


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


回答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.



来源:https://stackoverflow.com/questions/6051775/using-java-to-do-a-windows-command-line-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!