使用Runtime.exec / ProcessBuilder.start以低优先级启动Java进程? [英] Start a Java process at low priority using Runtime.exec / ProcessBuilder.start?

查看:574
本文介绍了使用Runtime.exec / ProcessBuilder.start以低优先级启动Java进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

有没有人知道如何从Java启动进程(特定于Windows,如果需要),低于正常优先级?

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

推荐答案

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

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.

等待:

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

这篇关于使用Runtime.exec / ProcessBuilder.start以低优先级启动Java进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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