-1

I want to recreate java -jar [jar] &>logfile & in java via the ProcessBuilder.

Here's my current code:

File outFile = new File(".."); // logfile
ProcessBuilder builder = new ProcessBuilder(args);  // args = ["java", "-jar", "[jar]"]
builder.redirectError(outFile);
builder.redirectOutput(outFile);
Process process = builder.start();

The said jar is a web server and is supposed to run forever. But when I exit the java process that spawned the server it is also terminated.

My question now is: How do I tell the ProcessBuilder to execute the command in a background thread that doesn't terminate when the caller exits?

Prepending the args with nohup has not changed this behavior, using:

args = ["nohup", "java", "-jar", "[jar]"]

I've also tried

args = ["nohup", "java", "-jar", "[jar]", "&"]

Both of these do not work, neither on windows or linux

  • Possible duplicate of [How to run a proccess completely seperate in Java](https://stackoverflow.com/questions/34841694/how-to-run-a-proccess-completely-seperate-in-java) – Progman Aug 04 '19 at 15:32
  • prepending `nohup` to the command did nothing – Philipp Hemmelmayr Aug 04 '19 at 15:38
  • Please edit your question on how you tried to use the `nohup` command and describe how this solution fails in your case. – Progman Aug 04 '19 at 15:48
  • Use "start" and/or "cmd" when you are on windows, see https://stackoverflow.com/questions/11394394/how-do-i-run-command-line-from-java-code-in-the-background – Progman Aug 04 '19 at 16:06

1 Answers1

0

According to https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html#tag_16_111_03 if parent processes ignore signals, their children will too.

So the easy fix here is to start my parent java process with nohup or append a &