操作系统命令工具Util
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; /**
* 操作系统命令工具
*/
public class ExecUtil {
private static final Logger logger = LogManager.getLogger(ExecUtil.class); /**
* 执行一条操作系统命令*/
public static CmdInfo exec(String command) {
logger.info("exec cmd begin: " + command);
try{
Process ps = Runtime.getRuntime().exec(command);
ps.waitFor();
StreamGobbler errorGobbler = new StreamGobbler(ps.getErrorStream());
errorGobbler.start();
StreamGobbler outGobbler = new StreamGobbler(ps.getInputStream());
outGobbler.start();
outGobbler.join();
CmdInfo info = new CmdInfo(ps.exitValue(), errorGobbler.getInfo(), outGobbler.getInfo());
return info;
}
catch(Exception e){
logger.info("exec cmd error: ", e);
throw new RuntimeException(e);
}
finally{
Runtime.getRuntime().runFinalization();
}
} public static CmdInfo exec(String[] command) { try{
Process ps = Runtime.getRuntime().exec(command);
ps.waitFor();
StreamGobbler errorGobbler = new StreamGobbler(ps.getErrorStream());
errorGobbler.start();
StreamGobbler outGobbler = new StreamGobbler(ps.getInputStream());
outGobbler.start();
outGobbler.join();
CmdInfo info = new CmdInfo(ps.exitValue(), errorGobbler.getInfo(), outGobbler.getInfo());
return info;
}
catch(Exception e){
logger.info("exec cmd error: ", e);
throw new RuntimeException(e);
}
finally{
Runtime.getRuntime().runFinalization();
// Runtime.getRuntime ().gc();
}
} public static boolean execCmdAndWait(String cmd, long timeout, TimeUnit unit) {
try {
Process process = Runtime.getRuntime().exec(new String[]{"/bin/bash", "-c", cmd});
process.waitFor(timeout, unit);
} catch (InterruptedException | IOException e) {
logger.error("exec cmd: " + cmd + " and wait " + timeout + " " + unit + " go wrong", e);
return false;
}
return true;
}/**
* 命令信息
*/
public static class CmdInfo implements Serializable { private static final long serialVersionUID = 7416244152344549775L;
private int exitValue;
private String errorInfo;
private String msgInfo; public CmdInfo(int exitValue, String errorStreamInfo, String inputStreamInfo) {
this.exitValue = exitValue;
this.errorInfo = errorStreamInfo;
this.msgInfo = inputStreamInfo;
} public String getErrorInfo() { return errorInfo;
}
public void setErrorInfo(String errorStreamInfo) { this.errorInfo = errorStreamInfo;
} public int getExitValue() { return exitValue;
} public void setExitValue(int exitValue) { this.exitValue = exitValue;
} public String getMsgInfo() { return msgInfo;
} public void setMsgInfo(String inputStreamInfo) { this.msgInfo = inputStreamInfo;
} } /**
* 处理Process的stdout和stderr的类
*/
static class StreamGobbler extends Thread { private InputStream is; private OutputStream os; private String info = ""; public StreamGobbler(InputStream is) { this(is, null);
} public StreamGobbler(InputStream is, OutputStream redirect) { this.is = is;
this.os = redirect;
} public String getInfo() { return this.info;
} /*
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
public void run() { PrintWriter pw = null;
InputStreamReader isr = null;
BufferedReader br = null;
try{
pw = null;
if(os != null){
pw = new PrintWriter(os);
} isr = new InputStreamReader(is);//,"GBK"
br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null){
if(pw != null){
pw.println(line);
}
info += line + "\n";
// System.out.println (line);
}
if(pw != null){
pw.flush();
}
}
catch(IOException ioe){
throw new RuntimeException(ioe);
}
finally{
try{
if(pw != null){
pw.close();
}
if(isr != null){
isr.close();
}
if(br != null){
br.close();
}
}
catch(IOException e){
throw new RuntimeException(e);
}
}
}
} }
操作系统命令工具Util的更多相关文章
- 自己封装的poi操作Excel工具类
自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...
- 自己的包poi操作Excel工具
在前面的文章<使用poi读写Excel>中分享了一下poi操作Excel的简单演示样例.这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完毕的功能是:读取Excel.汇总E ...
- java日期操作常用工具
java日期操作常用工具 package com..util; import java.sql.Timestamp; import java.text.SimpleDateFormat; import ...
- Java操作Redis工具类
依赖 jar 包 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis< ...
- Redis操作Set工具类封装,Java Redis Set命令封装
Redis操作Set工具类封装,Java Redis Set命令封装 >>>>>>>>>>>>>>>>& ...
- Redis操作List工具类封装,Java Redis List命令封装
Redis操作List工具类封装,Java Redis List命令封装 >>>>>>>>>>>>>>>> ...
- Redis操作Hash工具类封装,Redis工具类封装
Redis操作Hash工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>> ...
- Redis操作字符串工具类封装,Redis工具类封装
Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...
- java中文件操作的工具类
代码: package com.lky.pojo; import java.io.BufferedReader; import java.io.BufferedWriter; import java. ...
随机推荐
- DIV+CSS网页布局常用的一些基础知识
CSS命名规范 一.文件命名规范 全局样式:global.css:框架布局:layout.css:字体样式:font.css:链接样式:link.css:打印样式:print.css: 二.常用类/I ...
- element-ui的layout将24等分换为48等分
按住ctr箭点击element-ui/packages/theme-chalk/src/index";,再按住ctr贱点击col.scss跳转,将跳转到的col.scss中的24换为48(c ...
- iOS开发系列-修改项目工程名
当前有项目工程名为iOS,需要修改工程名为IFLY.在修改前注意备份项目 修改项目名 出现弹框,点击Rename 修改工程目录文件名 注意Tests与UITests不要删除 选中IFLY.xcodep ...
- gmock 简单笔记
std::shared_ptr<MockThreadRCInvester> spMockaAcc; HelperThreadRCInvester helperAcc; // spMockA ...
- 安装Epson打印机但因lsb依赖错误而中断的驱动程序
sudo apt-get install printer-driver-escpr 安装Epson打印机但因lsb依赖错误而中断的驱动程序 问题: 我正在安装 Epson XP-310 驱动程序,从这 ...
- CSIC_716_20191126【面向对象编程--继承】
继承 什么是继承:继承是新建类的一种方式,通过此方式生成的类称为子类.或者 派生类,被继承的类称为父类.基类或超类.在python中,一个子类可以继承多个父类. 继承的作用:减少代码的冗余,提高开发效 ...
- Erlang学习记录:转义
转义 转义序列 含义 整数编码 \b 退格符 8 \d 删除符 127 \e 换码符 27 \f 换页符 12 \n 换行符 10 \r 回车符 13 \s 空格符 32 \t 制表符 9 \v 垂直 ...
- 【JZOJ4811】排队
description analysis 堆\(+\)树上倍增 考虑后序遍历搞出\(dfs\)序,那么要填肯定是从\(dfs\)序开始填 把每个点是序里第几位看成优先级,用小根堆来维护当前空着的优先级 ...
- web Magic报错 NoSuchMethodError NoSuchMethodError: com.google.common.util.concurrent.SimpleTimeLimiter
webMagic使用selenium的时候遇到报错: java.lang.NoSuchMethodError: com.google.common.util.concurrent.SimpleTime ...
- echarts 默认柱状图每根柱子显示不同颜色(随机显示和定制显示)
series: [{ name: '请求数', type: 'bar', //barGap: 60, barWidth: 140,//柱图宽度 //stack: 'sum',//堆叠效果 itemSt ...