歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> linux上java調用shell腳本

linux上java調用shell腳本

日期:2017/3/3 13:01:08   编辑:SHELL編程

// java調用shell腳本

executeShellData(edepShellPath, businessDate);

方法一:使用Runtime.getRuntime().exec()

/**

* 方法簡述:java執行shell腳本

* <p>

* 方法詳細描述:使用Runtime.getRuntime().exec()

*

* @Since: 2016-04-11

* @param execPath

* @param businessDate

* @return

*/

private void executeShellData(String execPath, String businessDate) {

LOGGER.info("executeShellData貼源報表統計執行shell腳本開始!execPath[{}],businessDate[{}]",execPath, businessDate);

InputStreamReader isr = null;

String result = null;

try {

Process proc = Runtime.getRuntime().exec(

"sh " + execPath + " " + businessDate);

BufferedReader br = null;

StringBuffer sb = new StringBuffer();

try {

isr = new InputStreamReader(proc.getInputStream(),

Constant.CORE_FILE_CHARACTERSET_GBK);

br = new BufferedReader(isr);

String line = null;

while ((line = br.readLine()) != null) {

LOGGER.info("executeShellData br.readLine()=[{}]", line);

sb.append(line).append(Constant.UNIX_LINE_FEED);

}

result = sb.toString();

} catch (Exception e) {

LOGGER.error(

"executeShellData貼源報表統計執行shell腳本獲取輸入流失敗!result[{}]",

result, e);

} finally {

if (br != null) {

br.close();

}

if (isr != null) {

isr.close();

}

}

proc.destroy();

} catch (IOException e) {

LOGGER.error("executeShellData貼源報表統計執行shell腳本,啟動進程失敗!", e);

} catch (Exception e) {

LOGGER.error("executeShellData貼源報表統計,執行shell腳本失敗!result[{}]",

result, e);

}

LOGGER.info(

"executeShellDataEnd貼源報表統計,java調用shell腳本結束!edepShellPath[{}], result[{}]",

execPath, result);

}

Copyright © Linux教程網 All Rights Reserved