0

Using process builder opening the CMD and execute a particular file in D drive.

Below is my initial code

this is CMD opening with my current working directory (IDE project location initially :C:\Users\xxx\yyy\testjson>)

ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C", "start");
        Process p = pb.start();

I want to add the below cmd command in my code, and execute a particular file in that folder, let me know how can?

1.change the directory to D
2.move to the particular folder into that D drive
Prabu
  • 3,550
  • 9
  • 44
  • 85

1 Answers1

2

The method you are looking for is ProcessBuilder::directory(String) which sets the working directory for the new process; see the javadoc.

   Process p = new ProcessBuilder("cmd.exe", "/C", "start")
                .directory(new File("D:/some/directory"))
                .start();
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216