Mercurial > hg > Members > nobuyasu > TestJava
view src/testKonoha/KonohaInvoker.java @ 3:178aeb91d108 draft default tip
add pom.xml
author | one |
---|---|
date | Mon, 04 Jun 2012 13:53:39 +0900 |
parents | b101ee77e78c |
children |
line wrap: on
line source
package testKonoha; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class KonohaInvoker { final static String KONOHA = "/usr/local/bin/konoha"; InputStream in, errin; void runKonoha(String[] cmd) throws KonohaException { try { Runtime run = Runtime.getRuntime(); Process p = run.exec(cmd); in = p.getInputStream(); errin = p.getErrorStream(); BufferedReader br = new BufferedReader(new InputStreamReader(in)); BufferedReader ebr = new BufferedReader(new InputStreamReader(errin)); String kout = readMessege(br); String kerr = readMessege(ebr); p.waitFor(); System.out.println(kerr); System.out.println(kout); if (kerr != "") { throw new KonohaException(kerr); } } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } finally { try { in.close(); errin.close(); } catch (Exception e) { e.printStackTrace(); } } } void invoke(String filename, String message) throws KonohaException { String[] cmd = {KONOHA, filename, message}; runKonoha(cmd); } void invoke(String filename, String[] messages) throws KonohaException { int num = messages.length + 2; String[] cmd = new String[num]; cmd[0] = KONOHA; cmd[1] = filename; for (int i=0; i < messages.length; i++) { cmd[i+2] = messages[i]; } runKonoha(cmd); } void invoke(String[] messages) throws KonohaException { int num = messages.length + 1; String[] cmd = new String[num]; cmd[0] = KONOHA; for (int i=0; i < messages.length; i++) { cmd[i+1] = messages[i]; } runKonoha(cmd); } String readMessege(BufferedReader br) throws IOException { String str = ""; String line = null; while ((line = br.readLine()) != null) { str = str + line + "\n"; } return str; } public static void main(String[] args) { KonohaInvoker konoha = new KonohaInvoker(); String filename = "/Users/aotokage/hg/nobuyasu/konoha/test/math.k"; String message = "64"; // String[] message = {"2","4"}; try { konoha.invoke(filename, message); } catch (KonohaException e) { System.out.println(e); } } }