java執(zhí)行bat命令碰到的阻塞問題的解決方法
使用Java來執(zhí)行bat命令,如果bat操作時間過長,有可能導(dǎo)致阻塞問題,而且不會執(zhí)行bat直到關(guān)閉服務(wù)器。
如:
Runtime r=Runtime.getRuntime();
Process p=null;
try{
String path = "D:/test.bat";
p = r.exec("cmd.exe /c "+path);
p.waitFor();
}catch(Exception e){
System.out.println("運行錯誤:"+e.getMessage());
e.printStackTrace();
}
一般java的exec是沒有幫你處理線程阻塞問題的,需要手動處理。
處理后:
Runtime r=Runtime.getRuntime();
Process p=null;
try{
String path = "D:/test.bat";
p = r.exec("cmd.exe /c "+path);
StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");
errorGobbler.start();
StreamGobbler outGobbler = new StreamGobbler(p.getInputStream(), "STDOUT");
outGobbler.start();
p.waitFor();
}catch(Exception e){
System.out.println("運行錯誤:"+e.getMessage());
e.printStackTrace();
}
StreamGobbler 類如下:
package com.test.tool;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
/**
* 用于處理Runtime.getRuntime().exec產(chǎn)生的錯誤流及輸出流
*/
public class StreamGobbler extends Thread {
InputStream is;
String type;
OutputStream os;
StreamGobbler(InputStream is, String type) {
this(is, type, null);
}
StreamGobbler(InputStream is, String type, OutputStream redirect) {
this.is = is;
this.type = type;
this.os = redirect;
}
public void run() {
InputStreamReader isr = null;
BufferedReader br = null;
PrintWriter pw = null;
try {
if (os != null)
pw = new PrintWriter(os);
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null) {
if (pw != null)
pw.println(line);
System.out.println(type + ">" + line);
}
if (pw != null)
pw.flush();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally{
try {
pw.close();
br.close();
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
運行bat,就不會阻塞了。
相關(guān)文章
springboot 在idea中實現(xiàn)熱部署的方法
這篇文章主要介紹了springboot 在idea中實現(xiàn)熱部署的方法,實現(xiàn)了熱部署,在每一次作了修改之后,都會自動的重啟,非常節(jié)約時間,感興趣的小伙伴們可以參考一下2018-10-10
SpringBoot2.1.x,創(chuàng)建自己的spring-boot-starter自動配置模塊操作
這篇文章主要介紹了SpringBoot2.1.x,創(chuàng)建自己的spring-boot-starter自動配置模塊操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Springboot結(jié)合@validated優(yōu)化代碼驗證
這篇文章主要介紹了Springboot與@validated注解結(jié)合從而實現(xiàn)讓你的代碼驗證更清爽,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Spring?Boot如何實現(xiàn)統(tǒng)一數(shù)據(jù)返回
這篇文章主要介紹了Spring?Boot如何實現(xiàn)統(tǒng)一數(shù)據(jù)返回,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2024-07-07
詳解SpringCloud Ribbon 負載均衡通過服務(wù)器名無法連接的神坑
這篇文章主要介紹了詳解SpringCloud Ribbon 負載均衡通過服務(wù)器名無法連接的神坑,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06
java同步器AQS架構(gòu)AbstractQueuedSynchronizer原理解析
這篇文章主要為大家介紹了java同步器AQS架構(gòu)AbstractQueuedSynchronizer的底層原理及源碼解析,有需要的朋友可以借鑒參考下,希望能有所幫助,祝大家多多進步早日升職加薪2022-03-03
Java實現(xiàn)根據(jù)sql動態(tài)查詢并下載數(shù)據(jù)到excel
這篇文章主要為大家詳細介紹了如何使用Java實現(xiàn)根據(jù)sql動態(tài)查詢并下載數(shù)據(jù)到excel的功能,文中的示例代碼講解詳細,有需要的可以參考下2024-04-04
java開發(fā)SSM框架具有rest風(fēng)格的SpringMVC
這篇文章主要介紹了java開發(fā)中如何使SSM框架具有rest風(fēng)格的SpringMVC實現(xiàn)解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10

