当前位置:网站首页>ProcessBuilder工具类
ProcessBuilder工具类
2022-04-23 06:08:00 【Johnny2004】
一.代码
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
*
* *
* * 测试方式:
* *
* * 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();
/**
* 执行脚本命令
* @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;
}
/**
* 获取错误信息
* @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;
}
/**
* 关流
* @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();
}
}
/**
* 终止进程
* @param process
* @return void
* @date 2019/9/26
* @throws
*/
public static void destroy(Process process) {
if (process != null) {
process.destroyForcibly();
}
}
}
二.注意
如果出现"ProcessBuilder java.io.IOException: Cannot run program error=2, No such file or directory" 错误,则可以使用
List<String> commend = new ArrayList<>();
commend.add("/bin/sh");
commend.add("-c");
commend.add("第1条命令","第2条命令","第3条命令"); //这里可以实现执行多条linux命令
Process process = ProcessBuilder.exec(commend);
String message = ProcessBuilder.getOutput(process);
String error = ProcessBuilder.getError(process);
ProcessBuilder.destroy(process);
版权声明
本文为[Johnny2004]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Johnny2004/article/details/121938178
边栏推荐
猜你喜欢
随机推荐
微服务架构核心概念
oracle undo使用率高问题处理
How does thanos configure different data retention durations for different tenants
mysql和pgsql时间相关操作
Exception record-14
webView因证书问题显示一片空白
专用窗口函数rank, dense_rank, row_number
Apprentissage par composantes
第三篇:docker安装mysql容器(自定义端口)
BottomSheetDialogFragment 与 ListView RecyclerView ScrollView 滑动冲突问题
常用于融合去重的窗口函数row_number
Itop4412 cannot display boot animation (4.0.3_r1)
oracle表空间表分区详解及oracle表分区查询使用方法
19C RAC修改VIP及SCANIP步骤-同网段
接口幂等性问题
记录webView显示空白的又一坑
Dolphinscheduler集成Flink任务踩坑记录
Static interface method calls are not supported at language level ‘5‘ 异常解决
JVM basics you should know
useReducer基本用法