当前位置:网站首页>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
边栏推荐
- 9419页最新一线互联网Android面试题解析大全
- ESP32 VHCI架构传统蓝牙设置scan mode,让设备能被搜索到
- playwright控制本地穀歌瀏覽打開,並下載文件
- AUTOSAR from introduction to mastery 100 lectures (81) - FIM of AUTOSAR Foundation
- What do the raddr and rport in webrtc ice candidate mean?
- Solve the problem of Oracle Chinese garbled code
- 初鉴canvas,展示个小小的小案例
- 十万大学生都已成为猿粉,你还在等什么?
- Pyqt5 store opencv pictures into the built-in sqllite database and query
- 叮~ 你的奖学金已到账!C认证企业奖学金名单出炉
猜你喜欢

AUTOSAR from introduction to mastery 100 lectures (51) - AUTOSAR network management

Imx6ull QEMU bare metal tutorial 1: GPIO, iomux, I2C

Lpddr4 notes

Solve the problem that Oracle needs to set IP every time in the virtual machine

叮~ 你的奖学金已到账!C认证企业奖学金名单出炉
![[dynamic programming] 221 Largest Square](/img/31/3af4ba05764a683220b5292d3b8780.png)
[dynamic programming] 221 Largest Square

You and the 42W bonus pool are one short of the "Changsha bank Cup" Tencent yunqi innovation competition!

MySQL 8.0.11 download, install and connect tutorials using visualization tools

Kernel error: no rule to make target 'Debian / canonical certs pem‘, needed by ‘certs/x509_ certificate_ list‘

CSDN College Club "famous teacher college trip" -- Hunan Normal University Station
随机推荐
According to the salary statistics of programmers in June 2021, the average salary is 15052 yuan. Are you holding back?
The first lesson is canvas, showing a small case
[51 single chip microcomputer traffic light simulation]
Hbuilderx + uniapp packaging IPA submission app store stepping on the pit
C语言之字符串与字符数组的区别
[point cloud series] pointfilter: point cloud filtering via encoder decoder modeling
mui picker和下拉刷新冲突问题
PyTorch 21. NN in pytorch Embedding module
RTOS mainstream assessment
The difference between string and character array in C language
Lpddr4 notes
100 GIS practical application cases (53) - making three-dimensional image map as the base map of urban spatial pattern analysis
7_ The cell type scores obtained by addmodule and gene addition method are compared in space
(personal) sorting out system vulnerabilities after recent project development
Ding ~ your scholarship has arrived! C certified enterprise scholarship list released
How do ordinary college students get offers from big factories? Ao Bing teaches you one move to win!
MySQL5.5安装教程
[point cloud series] Introduction to scene recognition
[untitled] PID control TT encoder motor
ESP32 VHCI架构传统蓝牙设置scan mode,让设备能被搜索到