java执行cmd命令

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
/**
 * 执行cmd命令
 * @author kk
 *
 */
public class CMD {

	public static void main(String[] args) {
		try {	
			String cmd = "ipconfig";//cmd命令
			String cmd = "exp kk/1234@orcl file=d:\\databaseBackup\\edu.dmp owner=(kk)";//cmd命令
					
			ArrayList<String> commands = new ArrayList<String>();
			
//			cmd.split(" ") 命令行按照空格分割,放入集合中
			commands.addAll(Arrays.asList(cmd.split(" ")));
			//commands.add(cmd);
		
			ProcessBuilder pb = new ProcessBuilder(commands);
			pb.redirectErrorStream(true);
			Process p = pb.start();//启动进程
			
			BufferedReader is = new BufferedReader(new InputStreamReader(p.getInputStream(),"gbk"));
			String line;
			while ((line = is.readLine()) != null) {
				if (line.toLowerCase().startsWith("warning")) {
					System.err.println("\tWARNING: " + line);
				} else if (line.toLowerCase().startsWith("error")) {
					System.err.println("\tERROR: " + line);
				} else if (line.toLowerCase().startsWith("fatal")) {
					System.err.println("\tFATAL ERROR: " + line);
				} else {
					System.out.println("\t" + line);
				}
			}
			try {
				p.waitFor();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("结束");
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值