Question

This is one way to run cmd.exe using java:

String command="cmd /c start cmd.exe";
Process p = Runtime.getRuntime().exec(command);

How to enforce command to run cmd.exe from the root directory C:\ ?

Was it helpful?

Solution

As suggested by others, consider using ProcessBuilder.

Code:

ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/C", "start");
processBuilder.directory(new File("C:\\"));
try {
    processBuilder.start();
} catch (IOException e) {
    e.printStackTrace();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top