Mercurial > hg > Members > nobuyasu > konoha
changeset 10:888143c20a4c
add System.java
author | Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 27 May 2012 21:06:26 +0900 |
parents | b1dc0a0565f2 |
children | f2fc4689d3d6 |
files | SystemTest.java |
diffstat | 1 files changed, 98 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /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(); + } + + } + + +}