I have this code to insert an executable to the start up registry:
private static void addToWin( File f, String param ) throws IOException {
String name = generateName(f);
String cmd = "reg add HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /v " + name + " /t REG_SZ /d \"" + f.getAbsolutePath() + param + "\"";
Runtime.getRuntime().exec(cmd);
}
This works, but the problem is that after running it I have a process in task manager called reg.exe that takes the 10% of the CPU. This prevent the JVM to shutdown at the end of the running ( even with a System.exit() at the end )
The code that removes the same entry from the registry works well and does not have this problem.
Do you have an idea of what is going on and how to solve this?
Thankyou
I seem to remember having a similar issue and finding out that the process would not terminate until its output was consumed. Try keeping a reference to the
Processobject returned by the runtime exec method, like so…… then get an input stream from the Process and simply read from it until it ends.
Error handling needs to be added. You might have to do the same for stderr, not only stdout.
EDIT: oh, and certainly don’t forget to close the stream. Seems like the kind of thing that holds on to system resources.