machinery
machinery

Reputation: 6290

Java Runtime.exec communictation possible?

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

Upvotes: 1

Views: 157

Answers (1)

aioobe
aioobe

Reputation: 421340

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.

Upvotes: 1

Related Questions