13

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?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Andrew
  • 617
  • 2
  • 6
  • 19
  • 2
    because `start` is not executable, but internal command of `cmd.exe` – Op De Cirkel Jun 03 '11 at 06:41
  • The only thing i can think of is to wrap it in .bat and in the bat file use start – Op De Cirkel Jun 03 '11 at 06:45
  • I think you meant .cmd instead of .bat – rakslice Aug 09 '11 at 03:51
  • @OpDeCirkel, i have a question. I never knew about start command affecting process priority. If i want to have admin access, so let's say i am on elevated cmd line, then if i use start jar-file, then also my file will be running with low priority but with elevated rights, right? – Johnydep Jan 31 '12 at 16:06

1 Answers1

16

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");
Community
  • 1
  • 1
teodozjan
  • 913
  • 10
  • 35
  • While this does run the process in low priority as requested it creates a disconnect between the Process object and the actual java payload. Which means that with /C it appears to finish immediately (allowing a new job to be picked up leading to massive spam of work), or with /K it persists and appears to never finish even though the payload has completed. – Andrew Jun 04 '11 at 22:34
  • The expected running behaviour can be restored by adding /wait to the start command and removing /b, however, the Process.destroy() method remains ineffectual. – Andrew Jun 04 '11 at 23:29
  • Standard Admins Answer #1 Works on my computer. See my edits. Maybe your third party app ignores kill signal -- than this is topic for another question. How to prevent blocking kill signal for process in windows. – teodozjan Jun 06 '11 at 08:06
  • Substituted with "Before start" to make this output more clear. – teodozjan Jun 14 '11 at 21:43
  • A destroy on the process will not necessarily kill the subprocess. Using the winp library should provide a better solution: http://winp.kohsuke.org/ – parasietje Aug 14 '13 at 19:39