Use a ProcessBuilder
. Unlike Runtime.exec()
, which you really should not use at all in modern code, a ProcessBuilder
allows you to set up the directory in which the spawned process will run. Runtime.exec()
does not give you this option.
Therefore:
final Path basePath = Paths.get("C:\\dir1\\dir2\\dir3");
final Process compileProcess = new ProcessBuilder("javac", "Main.java")
.directory(basePath.toFile())
// other niceties of ProcessBuilder
.start();
// check the status of the process; rinse, repeat