import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MainClass {

    public static void main(String[] args) throws Exception {
    String runPath = "C:/test/";
    String encode = "big5";
    String className = "TestClass";
    execErroutput(runPath, encode, className);
    execOutput(runPath, encode, className);
}

static void execErroutput(String runPath, String encode, String className) throws Exception {
    Runtime rt = Runtime.getRuntime();

    Process proc = rt.exec("javac " + className + ".java", new String[]{}, new File(runPath));
    InputStream stderr = proc.getErrorStream();
    InputStreamReader isr = new InputStreamReader(stderr, encode);
    BufferedReader br = new BufferedReader(isr);
    String line = null;
    while ((line = br.readLine()) != null) {
        System.err.println(line);
    }
    System.out.println();
}

static void execOutput(String runPath, String encode, String className) throws Exception {
    Runtime rt = Runtime.getRuntime();

    Process proc = rt.exec("java " + className, new String[]{}, new File(runPath));
    InputStream stderr = proc.getInputStream();
    InputStreamReader isr = new InputStreamReader(stderr, encode);
    BufferedReader br = new BufferedReader(isr);
    String line = null;
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
}

}

// 以下檔案我放在 C:\test\TestClass.java

public class TestClass {

    public static void main(String[] args) throws Exception {
        System.out.println("Hello Loli");
        System.out.println("Hello Kitty");
        System.out.println("Hello World");
    }
}

 







arrow
arrow
    全站熱搜

    黃彥霖 發表在 痞客邦 留言(0) 人氣()