使用Java运行时添加注册表项,导致进程reg.exe永远运行 [英] Using Java runtime to add registry key, cause process reg.exe to run forever

查看:415
本文介绍了使用Java运行时添加注册表项,导致进程reg.exe永远运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码将可执行文件插入启动注册表:

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);
}

这有效,但问题是在运行之后我有一个进程任务管理器调用reg.exe,占用CPU的10%。这可以防止JVM在运行结束时关闭(即使最后使用System.exit())

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?

谢谢

推荐答案

我似乎记得有一个类似的问题,并发现该过程不会终止,直到它的输出被消耗了。尝试保持对运行时exec方法返回的 Process 对象的引用,如下所示......

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);

...然后从流程中获取输入流,只需从中读取,直到结束。 / p>

... then get an input stream from the Process and simply read from it until it ends.

InputStream ips = proc.getInputStream();
while(ips.read() != -1) {}

错误处理需求待补充。你可能不得不为stderr做同样的事,不仅仅是stdout。

Error handling needs to be added. You might have to do the same for stderr, not only stdout.

编辑:哦,当然不要忘记关闭流。看起来像是系统资源的那种东西。

oh, and certainly don't forget to close the stream. Seems like the kind of thing that holds on to system resources.

这篇关于使用Java运行时添加注册表项,导致进程reg.exe永远运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆