Launch the application and record its input / output streams, and then pass through it a serialized model of the object. The new application should deserialize the input coming from System.in.
Concept example (I just wanted to make sure my example works, sorry for the delay):
package tests; import java.io.File; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class AppFirst { public static void main(String[] args) throws Exception { ProcessBuilder pb = new ProcessBuilder("java", "-cp", "./bin", "tests.AppSecond"); pb.directory(new File(".")); pb.redirectErrorStream(true); Process proc = pb.start(); ObjectOutputStream out = new ObjectOutputStream( proc.getOutputStream()); ObjectInputStream oin = null; for (int i = 0; i < 1000; i++) { out.writeObject("Hello world " + i); out.flush(); if (oin == null) { oin = new ObjectInputStream(proc.getInputStream()); } String s = (String)oin.readObject(); System.out.println(s); } out.writeObject("Stop"); out.flush(); proc.waitFor(); } }
package tests; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class AppSecond { public static void main(String[] args) throws Exception { ObjectInputStream oin = null; ObjectOutputStream out = new ObjectOutputStream(System.out); while (true) { if (oin == null) { oin = new ObjectInputStream(System.in); } String s = (String)oin.readObject(); if ("Stop".equals(s)) { break; } out.writeObject("Received: " + s); out.flush(); } } }
Edit: Added cyclic version. Please note that there should be a trick in OOS, as it immediately starts reading the transmitted stream (and blocks your application. If you do this at the wrong step, it should be wrapped AFTER the first object is sent to the child of the process).
source share