如何使用java來執行Shell Script,底下是個簡單例子,如果有亂碼問題,請記得在reader要指定編碼來解決亂碼困擾。

import java.io.*;
import java.net.*;
import java.util.*;
public class runshell {
    public static void main(String a[]) throws Exception{
        String cmd = "/bin/ls";
        Process pl = Runtime.getRuntime().exec(cmd);
        String line = "";
        BufferedReader p_in = new BufferedReader(new InputStreamReader(pl.getInputStream()));
        while ((line = p_in.readLine()) != null) {
            System.out.println(line);
        }
        p_in.close();
    }
}

mark528 發表在 痞客邦 PIXNET 留言(0) 人氣()