java 执行shell命令



import java.io.BufferedReader;
import java.io.InputStreamReader;

public class shell {
    public static String executeCommand(String command) {
        StringBuffer output = new StringBuffer();
        Process p;
        try {
            p = Runtime.getRuntime().exec(command);
            BufferedReader reader =
                    new BufferedReader(new InputStreamReader(p.getInputStream()));

            BufferedReader reader1 =
                    new BufferedReader(new InputStreamReader(p.getErrorStream()));

            String line;
            while ((line = reader.readLine())!= null) {

                output.append(line + "\n");
                System.out.println("info:" + line);
                String err = reader1.readLine();
                if(err != null){
                    output.append(line + "\n");
                    System.out.println("error:" + err);

                }
            }
            reader.close();
            reader1.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return output.toString();
    }
}


你可能感兴趣的:(java 执行shell命令)