操作系统命令工具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. ...
随机推荐
- jenkins深入浅出
安装: 1. 从官网上下载新版本的Jenkins,https://mirrors.tuna.tsinghua.edu.cn/jenkins/war-stable/2.89.4/jenkins.war ...
- 我写的界面,在ARM上跑
这个...其实,我对ARM了解并不多,我顶多也就算是知道ARM怎么玩,EMMC干啥,MMU干啥,还有早期的叫法,比如那个NorFlash NandFlash ,然后也就没啥了. 然后写个裸机什么的,那 ...
- JavaScript——问卷星自动填写
一.前言: 我们学校要刷学术章,有些学术章又是指定在某个时间点填写问卷星的问卷报名的.但是由于我手速慢,导致总会有些时候报不上名,于是想着搞个代码实现自动填写问卷星的报名表.一顿操作后,在github ...
- Web UI 设计(网页设计)命名规范
Web UI 设计命名规范 一.网站设计及基本框架结构: 1. Container“container“ 就是将页面中的所有元素包在一起的部分,这部分还可以命名为: “wrapper“, “wr ...
- Git查看历史记录的几种方法
- range和arange
a = np.arange(12) print(a, type(a)) b = range(10) print(b, type(b)) li = list(b) print(li) 拓展: 两个参数: ...
- mui--使用mui中的图文表格组件时出现一条横线的解决方法
最近做的微信公众号点击链接跳转到H5页面,该H5页面使用mui做的,遇到的商品列表页出现一个横线的问题, 这个是修改前的图片 解决方法: <style type="text/css&q ...
- JavaScript 点击事件的三种写法
嵌入式 <button οnclick='alert("hello")'>点击按钮</button> 脚本模型 btn.onclick=function() ...
- thinkphp 包含文件
在当前模版文件中包含其他的模版文件使用include标签,标签用法: <include file='模版表达式或者模版文件1,模版表达式或者模版文件2,...' /> 博智达直线电机价格 ...
- thinkphp 定界符
模板文件可以包含普通模板标签和XML模板标签,标签的定界符都可以重新配置. 大理石平台厂家 普通标签 内置模板引擎的普通模板标签默认以{ 和 } 作为开始和结束标识,并且在开始标记紧跟标签的定义,如果 ...