1 import java
.util
.Arrays
;
5 * Simple tool to start two processes piped into each other.
7 * @author Roman Elizarov
10 public static void main(String
[] args
) throws IOException
, InterruptedException
{
11 if (args
.length
< 2) {
12 System
.err
.println("Usage: run2 <interaction-exe> <main-exe> [<main-process-args> ...]");
15 run2 instance
= new run2(args
);
19 instance
.killThemAll();
23 private final Process p1
;
24 private final Process p2
;
26 private run2(String
[] args
) throws IOException
{
27 ProcessBuilder pb1
= new ProcessBuilder(args
[0]);
28 ProcessBuilder pb2
= new ProcessBuilder(Arrays
.asList(args
).subList(1, args
.length
).toArray(new String
[args
.length
- 1]));
29 pb1
.redirectErrorStream(true);
30 pb2
.redirectErrorStream(true);
35 private void go() throws InterruptedException
{
36 new Redirector(p1
.getInputStream(), p2
.getOutputStream()).start();
37 new Redirector(p2
.getInputStream(), p1
.getOutputStream()).start();
43 private void killThemAll() {
48 private class Redirector
extends Thread
{
50 final OutputStream out
;
52 private Redirector(InputStream in
, OutputStream out
) {
58 byte[] buf
= new byte[4096];
61 while ((len
= in
.read(buf
)) > 0) {
62 out
.write(buf
, 0, len
);
65 } catch (IOException e
) {
66 System
.err
.println(e
.toString());