# HG changeset patch # User Nobuyasu Oshiro # Date 1338120386 -32400 # Node ID 888143c20a4cc769c7609159209626f4bba1945d # Parent b1dc0a0565f2ba470ea6dea9655323c212f30e15 add System.java diff -r b1dc0a0565f2 -r 888143c20a4c SystemTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SystemTest.java Sun May 27 21:06:26 2012 +0900 @@ -0,0 +1,98 @@ +package Test; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +public class SystemTest { + + private static InputStream in = null; + private static InputStream ein = null; + static BufferedReader ebr = null; + static BufferedReader br = null; + + public static void main(String[] argv) { + +// String[] cmd = {"/usr/local/bin/konoha","/Users/aotokage/testProgram/konoha/math.k"}; +// String[] cmd = {"/usr/local/bin/konoha","math.k"}; + String[] cmd = {"konoha","math.k"}; + Runtime run = Runtime.getRuntime(); + try { + Process p = run.exec(cmd); + in = p.getInputStream(); + ein = p.getErrorStream(); + + + Runnable inputStreamThread = new Runnable() { + public void run() { + String line; + try { + System.out.println("Thread stdRun start"); + br = new BufferedReader(new InputStreamReader(in)); + while ((line = br.readLine()) != null) { + System.out.println(line); + } + System.out.println("Thread stdRun end"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + }; + + Runnable errStreamThread = new Runnable() { + public void run() { + try { + System.out.println("Thread errRun start"); + ebr = new BufferedReader(new InputStreamReader(ein)); + String errLine; + while((errLine = ebr.readLine()) != null) { + System.out.println(errLine); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + }; + + Thread stdRun = new Thread(inputStreamThread); + Thread errRun = new Thread(errStreamThread); + stdRun.start(); + errRun.start(); + + int c = p.waitFor(); + + stdRun.join(); + errRun.join(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + in.close(); + ein.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + } + + public static void printInputStream(InputStream in) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(in)); + try { + for (;;) { + String line = br.readLine(); + if (line == null) break; + System.out.println(line); + } + + } finally { + br.close(); + } + + } + + +}