首页 > 编程语言 > 详细

java如何运行OS命令(转)

时间:2015-02-02 13:53:21      阅读:272      评论:0      收藏:0      [点我收藏+]

 

  • javac TestRunTime.java
  • java TestRunTime hostname // 执行“hostname”Linux命令
  • 即可看到输出

 

 

 

public class TestRunTime {
 
    public static void main(String[] args) throws IOException, InterruptedException {
        String cmd = "";
        
        if(args == null || args.length == 0){
            System.out.println("请输入命令行参数");
        }else{
            
            for(int i=0;i<args.length; i++){
                cmd += args[i] + " ";
            }
        }
        
 
        try {
            Process process = Runtime.getRuntime().exec(cmd);
 
            InputStreamReader ir = new InputStreamReader(process.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
 
            String line;
            while ((line = input.readLine()) != null) {
                System.out.println(line);
            }
        } catch (java.io.IOException e) {
            System.err.println("IOException " + e.getMessage());
        }
    }
}

http://www.cnblogs.com/alipayhutu/archive/2012/06/12/2546214.html



java如何运行OS命令(转)

原文:http://www.cnblogs.com/softidea/p/4267575.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!