I have .bat file with following content for example:
mkdir testDir
Now I put it to folder C:\temp Then I want to run it using java so I do following:
Runtime.getRuntime().exec("cmd /c start C:\\temp\\test.bat");
I expect that folder will be created in C:\temp like when I execute this file manually, but folder is being created in my workspace which is wrong. How can I fix it?
You need to specify the working directory when you run cmd
.
There are overloads of Runtime.exec()
which allow you to specify a working directory. For example:
Runtime.getRuntime().exec("cmd /c start C:\\temp\\test.bat", null,
new File("c:\\temp"));
Alternatively, you can use ProcessBuilder
to give rather more explicit control of various aspects of the process you're starting.
See more on this question at Stackoverflow