java 执行 linux、windows 命令

本文展示了一个使用Java运行外部命令的示例代码,通过Runtime.getRuntime().exec()方法执行ipconfig命令,展示了如何读取命令的输出和错误流,并进行了适当的资源关闭处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

public class Test2 {

	public static void main(String[] args) throws Exception {
		run("ipconfig", true);
	}
	
	public static void run(String command,final boolean printLog) {  
//		System.out.println("执行命令:" + command);
    	Process process = null ;
        try {  
        	process = Runtime.getRuntime().exec(command);     
    		final InputStream is1 = process.getInputStream();    
            final InputStream is2 = process.getErrorStream();    
            new Thread() {    
                public void run() {    
                    BufferedReader br = null;    
                    try {    
                    	br = new BufferedReader(new InputStreamReader(is1, "gbk"));
                        String lineB = null;    
                        while ((lineB = br.readLine()) != null) {    
                            if (printLog) {
                            	System.out.println(lineB);
                            }  
                        }    
                    } catch (IOException e) {    
                        e.printStackTrace();    
                    }finally{
                    	try {
	                    	if(is1!=null){
								is1.close();
	                    	}
	                    	if(br!=null){
	                    		br.close();
	                    	}
                    	} catch (IOException e) {
                    		// TODO Auto-generated catch block
                    		e.printStackTrace();
                    	}
                    }  
                }    
            }.start();    
            new Thread() {    
                public void run() {    
                    BufferedReader br2 = null;    
                    try {    
                    	br2 = new BufferedReader(new InputStreamReader(is2, "gbk"));
                        String lineC = null;    
                        while ((lineC = br2.readLine()) != null) {    
                        	if (printLog) {
                            	System.out.println(lineC);
                            }
                        }    
                    } catch (IOException e) {    
                        e.printStackTrace();    
                    }finally{
                    	try {
	                    	if(is2!=null){
	                    		is2.close();
	                    	}
	                    	if(br2!=null){
	                    		br2.close();
	                    	}
                    	} catch (IOException e) {
                    		// TODO Auto-generated catch block
                    		e.printStackTrace();
                    	}
                    }  
                }    
            }.start();   
            process.waitFor();   
            System.out.println("执行命令完成:" + command);
        } catch (Throwable e) {  
        	System.out.println("执行命令失败:" + command);
            e.printStackTrace();  
        }
    }
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值