1

I have a main java program which should launch other java programs in an own process using Runtime.exec(), e.g.

Runtime.exec("java -jar myapp.jar");

Is there a possibility to communicate with this new process, e.g. sending request, chaing fields...?

How can I shutdown this new created process? I think I get an handler back and thus can kill the process. But is there a nicer way?

If I kill the process, will the shutdownhook still be executed before the process is killed?

Runtime.getRuntime().addShutdownHook
aioobe
  • 413,195
  • 112
  • 811
  • 826
machinery
  • 5,972
  • 12
  • 67
  • 118
  • *"If I kill the process"* - Assuming that you are talking about the process started with `Runtime.exec`, then yes, as the process and the shutdown hook are two different concepts and are unrelated. You may also want to do some research into `ProcessBuilder` as well – MadProgrammer Oct 06 '14 at 22:21
  • +1 on the `ProcessBuilder`. – aioobe Oct 06 '14 at 22:22

1 Answers1

1

Is there a possibility to communicate with this new process, e.g. sending request, chaing fields...?

You can communicate with the process through the Process object returned by Runtime.exec. Just use Process.getInputStream/.getOutputStream.

If you want to invoke methods on the other Java process you could look into RMI ("Remote method invocation"). Another option is of course sockets. See this related answer.

There's no straight forward platform independent way of changing fields of the other Java process.

If I kill the process, will the shutdownhook still be executed before the process is killed?

Depends on how you kill it, but typically, yes, the shutdown hooks will be executed.

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • Thank you very much, Stackoverlfow rulez. ;) How should I kill the process so that the shutdownhook is still executed? If I want to terminate the process I started with Runtime.exec, is the only way to kill the process? – machinery Oct 07 '14 at 00:04
  • I would recommend you to let the started process read from `System.in` and wait for a line containing `"exit"` and upon receiving the exit command terminate. – aioobe Oct 07 '14 at 05:19
  • You can for instance add something as follows to the started process: `new Thread() { public void run() { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { while (!br.readLine().equals("exit")); System.exit(0); } catch (IOException e) { } } }.start();` – aioobe Oct 07 '14 at 05:22
  • Thank you aioobe. This sounds great. But how can I then send the "exit" command from my main program (the program which created the process)? Is there something like Process.getOutputStream? – machinery Oct 07 '14 at 08:36
  • Yes. There's a Process.getOutputStream. I linked to it from my answer (although I had a typo in teh link text) – aioobe Oct 07 '14 at 11:09
  • Thank you. Is there also a way to send messages to a thread? I.e. I have a java program running which creates some threads and at a certain point of time I want to send new settings to these threads or flag them somehow that they should read in new settings. – machinery Oct 07 '14 at 20:18
  • I would recommend using a `BlockingQueue`. The thread reads the queue and whoever needs to communicate with the thread can add messages on the queue. – aioobe Oct 07 '14 at 21:35
  • But then the thread has to check regularly if there is something in the queue. Is there no way to singal the thread that it should check the queue without the need for the thread to look always on the queue? – machinery Oct 08 '14 at 09:36
  • I don't think you have a clear picture of how you want it to work. Suppose you could signal the thread to check the queue. Should it somehow "pause" execution of the current statement? If the value on the queue instructs the thread to do a particular task, should it forget about what it was doing before? Or should it magically resume where it left off before the check-queue signal? The concept of interthread communication can be a bit hard to grasp. Perhaps you're after some form of ExecutorService? – aioobe Oct 08 '14 at 10:55
  • If my new process using readline() to read something from the main process will it block until he reads something (and gets idle) or do I have to use a while loop? – machinery Oct 12 '14 at 08:02
  • Hmm.. Not sure what you mean. Set up a minimal example and post a new question and I'll have a look at it. – aioobe Oct 12 '14 at 08:09
  • I will try it myself first. ;) But I have another problem. If my subprocess (generated from my main process via processbuilder) prints to the console, will this actually appears on the console or do the main process has to gab it? I want it to print to the console. – machinery Oct 13 '14 at 14:07
  • It depends on how you set it up. You can set it up so that the output of the subprocess prints the output on the original applications stdout. See documentation of ProcessBuilder. – aioobe Oct 13 '14 at 14:08