当前位置:网站首页>Processbuilder tool class
Processbuilder tool class
2022-04-23 13:19:00 【Johnny2004】
One . Code
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;
/**
* @Date :Created in 4:49 PM 2019/9/26
*
* *
* * Test method :
* *
* * public static void main(String[] args) {
* * List<String> commend = new ArrayList<>();
* * commend.add("java");
* * commend.add("-version");
* * Process process = ProcessBuilderUtil.exec(this.commend);
* * String message = ProcessBuilderUtil.getError(process);
* * String error = ProcessBuilderUtil.getError(process);
* * ProcessBuilderUtil.destroy(process);
* * }
* *
*/
public class ProcessBuilderUtil {
private static ProcessBuilder processBuilder = new ProcessBuilder();
/**
* Execute script command
* @param commend
* @return java.lang.Process
* @date 2019/9/26
* @throws
*/
public static Process exec(List<String> commend) {
Process process = null;
try {
String[] commends = new String[commend.size()];
commend.toArray(commends);
processBuilder.command(commends);
process = processBuilder.start();
} catch (Exception e) {
e.printStackTrace();
}
return process;
}
/**
*
* @param process
* @return java.lang.String
* @date 2019/9/26
* @throws
*/
public static String getOutput(Process process) {
String output = null;
BufferedReader reader = null;
try {
if (process != null) {
StringBuffer stringBuffer = new StringBuffer();
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while (reader.read() != -1){
stringBuffer.append("\n" + reader.readLine());
}
output = stringBuffer.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
closeQuietly(reader);
return output;
}
/**
* Get error messages
* @param process
* @return java.lang.String
* @date 2019/9/26
* @throws
*/
public static String getError(Process process) {
String errput = null;
BufferedReader reader = null;
try {
if (process != null) {
StringBuffer stringBuffer = new StringBuffer();
reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while (reader.read() != -1){
stringBuffer.append("\n" + reader.readLine());
}
errput = stringBuffer.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
closeQuietly(reader);
return errput;
}
/**
* Shut off flow
* @param reader
* @return void
* @date 2019/9/26
* @throws
*/
public static void closeQuietly(Reader reader) {
try {
if (reader != null) {
reader.close();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* Terminate the process
* @param process
* @return void
* @date 2019/9/26
* @throws
*/
public static void destroy(Process process) {
if (process != null) {
process.destroyForcibly();
}
}
}
Two . Be careful
If appear "ProcessBuilder java.io.IOException: Cannot run program error=2, No such file or directory" error , You can use
List<String> commend = new ArrayList<>();
commend.add("/bin/sh");
commend.add("-c");
commend.add(" The first 1 Bar command "," The first 2 Bar command "," The first 3 Bar command "); // Here, you can execute multiple linux command
Process process = ProcessBuilder.exec(commend);
String message = ProcessBuilder.getOutput(process);
String error = ProcessBuilder.getError(process);
ProcessBuilder.destroy(process);
版权声明
本文为[Johnny2004]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230608014957.html
边栏推荐
- [point cloud series] so net: self organizing network for point cloud analysis
- decast id.var measure. Var data splitting and merging
- mui + hbuilder + h5api模拟弹出支付样式
- hbuilderx + uniapp 打包ipa提交App store踩坑记
- Lpddr4 notes
- [point cloud series] relationship based point cloud completion
- "Xiangjian" Technology Salon | programmer & CSDN's advanced road
- 榜样专访 | 孙光浩:高校俱乐部伴我成长并创业
- MySQL basic statement query
- web三大组件之Filter、Listener
猜你喜欢
超40W奖金池等你来战!第二届“长沙银行杯”腾讯云启创新大赛火热来袭!
AUTOSAR from introduction to mastery lecture 100 (84) - Summary of UDS time parameters
AUTOSAR from introduction to mastery 100 lectures (51) - AUTOSAR network management
web三大组件之Servlet
GIS practical tips (III) - how to add legend in CASS?
RTOS mainstream assessment
The use of dcast and melt in R language is simple and easy to understand
Melt reshape decast long data short data length conversion data cleaning row column conversion
filter()遍历Array异常友好
在 pytorch 中加载和使用图像分类数据集 Fashion-MNIST
随机推荐
基于uniapp异步封装接口请求简介
Analysis of the latest Android high frequency interview questions in 2020 (BAT TMD JD Xiaomi)
Lpddr4 notes
CMSIS cm3 source code annotation
普通大学生如何拿到大厂offer?敖丙教你一招致胜!
9419页最新一线互联网Android面试题解析大全
Hbuilderx + uniapp packaging IPA submission app store stepping on the pit
Riscv MMU overview
These vscode extensions are my favorite
MySQL basic statement query
hbuilderx + uniapp 打包ipa提交App store踩坑记
STM32 tracking based on open MV
Mui wechat payment pit
office2021安装包下载与激活教程
9419 page analysis of the latest first-line Internet Android interview questions
2021年6月程序员工资统计,平均15052元,你拖后腿了吗?
AUTOSAR from introduction to mastery 100 lectures (50) - AUTOSAR memory management series - ECU abstraction layer and MCAL layer
「玩转Lighthouse」轻量应用服务器自建DNS解析服务器
EMMC / SD learning notes
The filter() traverses the array, which is extremely friendly