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:softwaredataNotepad++下的notepad++;

最后

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

经验分享 程序员 微信小程序 职场和发展