0

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?

Evgeny Makarov
  • 1,417
  • 1
  • 21
  • 41
  • Specifically, the [second answer](http://stackoverflow.com/a/6811578/474189) in that duplicate is likely the best and matches Jon's answer below. – Duncan Jones May 16 '14 at 14:27

1 Answers1

2

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194