使用 Runtime.exec / ProcessBuilder.start 以低优先级启动 Java 进程?

发布于 2024-11-11 17:39:47 字数 349 浏览 3 评论 0原文

我正在尝试使用 ProcessBuilder 类通过 Java 启动外部进程,这很有效。目前正在使用命令运行:

new ProcessBuilder("java", "-jar", jarfile, args);

我想做的就是这样,但是以低优先级启动进程。我的程序目前仅在 Windows 上运行,因此特定于窗口的解决方案对我来说没问题。一些研究建议我使用“start”命令,但是当我尝试从 Java 执行此操作时,它会给出一个异常,说它是一个无法识别的命令(相同的命令在 cmd.exe 中工作)。

有谁知道如何从 Java 启动一个进程(如果需要的话,特定于 Windows),优先级低于正常值?

I'm trying to start an external process via Java using the ProcessBuilder class, and that much works. Currently running using the command:

new ProcessBuilder("java", "-jar", jarfile, args);

What I would like to do is just this, but to start the process with low priority. My program is currently only running on Windows, so a window-specific solution is fine by me. Some research suggests I use the "start" command, but when I try doing this from Java it gives an exception saying it is an unrecognized command (the same command works from cmd.exe).

Does anyone know how to launch a process from Java (Windows-specific if need be), with belownormal priority?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兔小萌 2024-11-18 17:39:47

使用启动命令< /a>.它依赖于 Windows,但可以满足您的需要。我已经读过没有跨平台的方法。

ProcessBuilder pb = new ProcessBuilder("cmd", "/C start /B /belownormal javaws -version");
System.out.println("Before start");
Process start = pb.start();

甚至可以读取输入端错误流。

等待:

ProcessBuilder pb = new ProcessBuilder("cmd", "/C start /B /belownormal /WAIT javaws -sdasd");
System.out.println("Before start");
Process start = pb.start();
start.waitFor();
System.out.println("Done");

对于过早销毁:

ProcessBuilder pb = new ProcessBuilder("cmd", "/C start /B /belownormal /WAIT javaws -sdasd");
System.out.println("Before start");
Process start = pb.start();
start.destroy();
start.waitFor();

System.out.println("Done");

Use start command. It is windows dependent but does what you need. I have read there is no cross platform way for this.

ProcessBuilder pb = new ProcessBuilder("cmd", "/C start /B /belownormal javaws -version");
System.out.println("Before start");
Process start = pb.start();

It is even possible to read Input end Error streams.

To wait:

ProcessBuilder pb = new ProcessBuilder("cmd", "/C start /B /belownormal /WAIT javaws -sdasd");
System.out.println("Before start");
Process start = pb.start();
start.waitFor();
System.out.println("Done");

For premature destroy:

ProcessBuilder pb = new ProcessBuilder("cmd", "/C start /B /belownormal /WAIT javaws -sdasd");
System.out.println("Before start");
Process start = pb.start();
start.destroy();
start.waitFor();

System.out.println("Done");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文