@Slf4j
@Component
public class ProcessExecUtil {
public static String exec(String start) throws Exception{
//BufferedReader reader ;
Process process = Runtime.getRuntime().exec(start);
return waitFor(process);
}
public static String waitFor(Process process) throws Exception{
InputStream is1 = process.getInputStream();
InputStream is2 = process.getErrorStream();
String result=null;
BufferedReader br1 = new BufferedReader(new InputStreamReader(is1));
try{
String line = null;
while ((line = br1.readLine()) != null){
//可选打印脚本输出日志内容
System.out.println("ProcessExecUtil.execInput:"+line);
result=line;
}
}catch (IOException e){
e.printStackTrace();
}finally {
try {
is1.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
new Thread(){
@Override
public void run() {
}
}.start();
new Thread(){
@Override
public void run() {
BufferedReader br2 = new BufferedReader(new InputStreamReader(is2));
try{
String line = null;
while ((line = br2.readLine()) != null){
//可选打印脚本输出日志内容
System.out.println("ProcessExecUtil.execError:"+line);
}
}catch (IOException e){
e.printStackTrace();
}finally {
try {
is2.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}.start();
//最后的程序状态值
int i = process.waitFor();
System.out.println("ruturn value:"+i);
return result;
}
}