Reputation: 669
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
Upvotes: 1
Views: 2436
Reputation: 12019
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 Process
object returned by the runtime exec method, like so...
Process proc = Runtime.getRuntime().exec(cmd);
... then get an input stream from the Process and simply read from it until it ends.
InputStream ips = proc.getInputStream();
while(ips.read() != -1) {}
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.
Upvotes: 2
Reputation: 9174
That may occur if the process writes something to its standard output and you don't consume that output. See the Javadoc of java.lang.Process.
Upvotes: 2