java调用Command命令

-----------

 

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

/**
 * 此类用来执行Command命令
 * 
 * @author zhj
 *
 */
public class CommandHelper {
    private boolean execCommand(String cmd) {
        Process process = null;
        BufferedReader br = null;
        try {
            process = Runtime.getRuntime().exec(cmd);
            br = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (Exception e) {
            return false;
        } finally {
            try {
                process.destroy();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    public static void main(String[] args) {
        CommandHelper mCommandHelper = new CommandHelper();
        mCommandHelper.execCommand("ls /opt");
    }
}

 

 

 

 

--

posted @ 2015-10-27 15:11  HuijunZhang  阅读(1563)  评论(0编辑  收藏
中国