I need to add a new directory to the PATH environment variable in Java. I have used below code but it seems to launch a command shell and hangs which i don't want.
Is there an easy way to update the PATH environment variable from within JAVA code?
try {
Process proc = Runtime.getRuntime().exec("cmd set PATH=%PATH%;C:\\MyDir1");
proc.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = reader.readLine();
while (line != null) {
//Handle what you want it to do here
line = reader.readLine();
}
}
catch (IOException | InterruptedException e1) {
//Handle your exception here
}