Runtime.getRuntime().exec()的使用以及简单封装

前言

虽然简单,但是使用时也会踩坑,写个简单的记录。

API相关

五个常用的API:
1.Runtime.getRuntime().exec(String cmd)
直接输入一行cmd命令,例如:ipconfig /all 、java -version等
2.Runtime.getRuntime().exec(String [] cmd)
将命令分开输入,例如new String[] {“java”, “-version”}
3.getInputStream();
获得输入流
4.getErrorStream();
获得错误流
5.waitFor(timeout, TimeUnit);
防止卡死

简单实例

做了一个简单的实例,并做了简单的封装。

package lyrics;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;

/**
 * Execute system command class
 * 
 * @author lyrics
 * @since 2020/06/15
 */
public class ExecUtil {
	/**
	 * Execute system command
	 * 
	 * @param cmd command
	 * @param timeOut time out
	 * @return the result
	 * @throws IOException 
	 * @throws InterruptedException 
	 * @author lyrics 
	 * @since 2020/06/15
	 */
	public static String exec(String cmd,int timeOut) throws IOException, InterruptedException {	
		Process p = Runtime.getRuntime().exec(cmd);
		boolean res = p.waitFor(timeOut, TimeUnit.SECONDS);
		if(!res) {
			return "Time out";
		}
		InputStream inputStream = p.getInputStream();
		byte[] data = new byte[1024];
		String result = "";
		while(inputStream.read(data) != -1) {
			result += new String(data,"GBK");
		}
		if (result == "") {
			InputStream errorStream = p.getErrorStream();
			while(errorStream.read(data) != -1) {
				result += new String(data,"GBK");
			}
		}
		return result;
	}
	
	/**
	 * Execute system command
	 * 
	 * @param cmd
	 * @param timeOut time out
	 * @return the result
	 * @throws IOException 
	 * @throws InterruptedException 
	 * @author lyrics 
	 * @since 2020/06/15
	 */
	public static String exec(String [] cmd ,int timeOut) throws IOException, InterruptedException {
		Process p = Runtime.getRuntime().exec(cmd);
		boolean res = p.waitFor(timeOut, TimeUnit.SECONDS);
		if(!res) {
			return "Time out";
		}
		InputStream inputStream = p.getInputStream();
		byte[] data = new byte[1024];
		String result = "";
		while(inputStream.read(data) != -1) {
			result += new String(data,"GBK");
		}
		if (result == "") {
			InputStream errorStream = p.getErrorStream();
			while(errorStream.read(data) != -1) {
				result += new String(data,"GBK");
			}
		}
		return result;
	} 
	public static void main(String [] args) {
		// test 1: ping
		try {
			String res = ExecUtil.exec("ping www.baidu.com", 5);
			System.out.println(res);
		} catch (IOException | InterruptedException e) {
			e.printStackTrace();
		}
		// test 2: ping
		try {
			String res = ExecUtil.exec(new String [] {"ping","www.baidu.com"}, 5);
			System.out.println(res);
		} catch (IOException | InterruptedException e) {
			e.printStackTrace();
		}
		// test 3: ipconfig
		try {
			String res = ExecUtil.exec("ipconfig", 5);
			System.out.println(res);
		} catch (IOException | InterruptedException e) {
			e.printStackTrace();
		}
		// test 4: ipconfig /all
		try {
			String res = ExecUtil.exec(new String [] {"ipconfig","/all"}, 5);
			System.out.println(res);
		} catch (IOException | InterruptedException e) {
			e.printStackTrace();
		}
		// test 5: open exe file
		try {
			String res = ExecUtil.exec("D:/software/data/Notepad++/notepad++", 5);
			System.out.println(res);
		} catch (IOException | InterruptedException e) {
			e.printStackTrace();
		}
	}
}

测试结果

测试一和测试二:
在这里插入图片描述
测试三
在这里插入图片描述
测试四:
在这里插入图片描述
测试五:
直接打开,位于‪D:\software\data\Notepad++下的notepad++;

最后

几点经验:
1.exec(String[] cmd)不容易翻车
2.注意编码格式为GBK,不然会乱码
3.复制路径的时候从属性里复制,容易翻车,报找不到路径或者文件,推测是编码格式的问题。

  • 7
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值