在工作中,由于需求,需要完成讓java去調(diào)用.bat文件和.sh文件的操作,接下來開始編碼
此處用到了糊涂工具類
maven如下文章來源:http://www.zghlxwxcb.cn/news/detail-522939.html
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.10</version>
</dependency>
public void run(){
? ? ? ? String file = "D:\\pan";
? ? ? ? String cmd = "run";
? ? ? ? Runtime runtime = Runtime.getRuntime();
Process exec = null;
if (FileUtil.isWindows()){
exec = runtime.exec("cmd /c cd " + file + " && " + cmd + ".bat");
}else {
? ? exec = runtime.exec("bash " + cmd + ".sh",null,new File(file));
}
exec.waitFor();
//取得命令結(jié)果的輸出流
InputStream is = exec.getInputStream();
//用一個讀輸出流類去讀
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
? ? ? ? while((line = br.readLine())!=null){
? ? ? ? ? ? System.out.println(line);
? ? ? ? }
is.close();
isr.close();
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
由此代碼來執(zhí)行bat或sh腳本文件(PS:糊涂工具包用不用無所謂,只是用來判斷是否為windows系統(tǒng)的)文章來源地址http://www.zghlxwxcb.cn/news/detail-522939.html
到了這里,關(guān)于使用java執(zhí)行bat、sh腳本文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!