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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6223765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:58:53  来源:igfitidea点击:

Start a Java process at low priority using Runtime.exec / ProcessBuilder.start?

javawindowsprocessbuilderwindows-task-scheduler

提问by Andrew

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

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

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

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

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

有谁知道如何belownormal优先从 Java 启动进程(如果需要,则是特定于 Windows 的)?

回答by teodozjan

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

使用启动命令。它依赖于 Windows,但可以满足您的需求。我读过没有跨平台的方式。

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