当前位置:网站首页>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
边栏推荐
- Summary of request and response and their ServletContext
- async void 导致程序崩溃
- torch. Where can transfer gradient
- [point cloud series] unsupervised multi task feature learning on point clouds
- AUTOSAR from introduction to mastery 100 lectures (81) - FIM of AUTOSAR Foundation
- Data warehouse - what is OLAP
- 基于uniapp异步封装接口请求简介
- 在 pytorch 中加载和使用图像分类数据集 Fashion-MNIST
- AUTOSAR from introduction to mastery lecture 100 (84) - Summary of UDS time parameters
- The first lesson is canvas, showing a small case
猜你喜欢
![[point cloud series] pointfilter: point cloud filtering via encoder decoder modeling](/img/da/02d1e18400414e045ce469425db644.png)
[point cloud series] pointfilter: point cloud filtering via encoder decoder modeling

Vscode tips

C语言之字符串与字符数组的区别

普通大学生如何拿到大厂offer?敖丙教你一招致胜!

EMMC / SD learning notes
![[point cloud series] so net: self organizing network for point cloud analysis](/img/3c/6136b7aa322c42089f40ce13a2d747.png)
[point cloud series] so net: self organizing network for point cloud analysis

100000 college students have become ape powder. What are you waiting for?

"Xiangjian" Technology Salon | programmer & CSDN's advanced road

你和42W奖金池,就差一次“长沙银行杯”腾讯云启创新大赛!

Kernel error: no rule to make target 'Debian / canonical certs pem‘, needed by ‘certs/x509_ certificate_ list‘
随机推荐
These vscode extensions are my favorite
100 GIS practical application cases (34) - splicing 2020globeland30
初鉴canvas,展示个小小的小案例
AUTOSAR from introduction to mastery 100 lectures (52) - diagnosis and communication management function unit
[point cloud series] summary of papers related to implicit expression of point cloud
缘结西安 | CSDN与西安思源学院签约,全面开启IT人才培养新篇章
Summary of request and response and their ServletContext
AUTOSAR from introduction to mastery lecture 100 (84) - Summary of UDS time parameters
[Technical Specification]: how to write technical documents?
EMMC / SD learning notes
十万大学生都已成为猿粉,你还在等什么?
解决虚拟机中Oracle每次要设置ip的问题
PyTorch 21. NN in pytorch Embedding module
hbuilderx + uniapp 打包ipa提交App store踩坑记
ECDSA signature verification principle and C language implementation
4.22 study record (you only did water problems in one day, didn't you)
Imx6ull QEMU bare metal tutorial 1: GPIO, iomux, I2C
How to convert opencv pictures to bytes
Learning notes of AMBA protocol
What do the raddr and rport in webrtc ice candidate mean?