java shell 指令
/**
*
* @author baoyou E-mail:[email protected]
* @version 2016年11月2日 下午1:54:49
* desc:
*/
public class ShellProcess {
private static ShellProcess instance;
public static ShellProcess getInstance() {
if (instance == null) {
synchronized (ShellProcess.class) {
if (instance == null) {
instance = new ShellProcess();
}
}
}
return instance;
}
/**
* 執行相應shell腳本
* @param args 執行腳本的參數,[0]path:shell腳本路徑;[1~n]腳本入參
* @return 傳回碼,0:成功 1:失敗
*/
public int runShell(String[] args) {
int runRes = SystemGlobal.FAILED;
try {
Process process = Runtime.getRuntime().exec(args);//調用相應shell腳本
new StreamGobbler(process.getInputStream(), StreamGobbler.INFO).start();
new StreamGobbler(process.getErrorStream(), StreamGobbler.ERROR).start();
runRes = process.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
return runRes;
}
/**
* 執行相應shell指令
* @param cmd 執行的指令
* @return 傳回碼,0:成功 1:失敗
*/
public int runShell(String cmd) {
return runShell(cmd);
}
/**
* 自動根據運作時環境執行shell指令
* @param args shell指令以空格分割後的list
* @return 傳回碼,0:成功 1:失敗
*/
public int runShell(List<String> args) {
int runRes = SystemGlobal.FAILED;
try {
ProcessBuilder pb = new ProcessBuilder(args);
Process process = pb.start();
new StreamGobbler(process.getInputStream(), StreamGobbler.INFO).start();
new StreamGobbler(process.getErrorStream(), StreamGobbler.ERROR).start();
runRes = process.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
return runRes;
}
/**
* 執行shell指令,并擷取傳回結果
* @param args shell指令以空格分割後的list
* @return 執行shell指令後的傳回結果(按行分割後的list),如果發生異常,傳回空List
*/
public List<String> runShellWithResult(List<String> args) {
List<String> results = new ArrayList<String>();
try {
ProcessBuilder pb = new ProcessBuilder(args);
Process process = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String data = null;
while ((data = br.readLine()) != null) {
results.add(data);
}
new StreamGobbler(process.getInputStream(), StreamGobbler.INFO).start();
new StreamGobbler(process.getErrorStream(), StreamGobbler.ERROR).start();
process.waitFor();
} catch (Exception e) {
e.printStackTrace();
return new ArrayList<String>();
}
return results;
}
}
package com.taskschedule.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.log4j.Logger;
/**
*
* @author baoyou E-mail:[email protected]
* @version 2016年11月2日 下午1:54:57
* desc:
*/
public class StreamGobbler extends Thread {
private InputStream is;
private int type;
private static Logger logger = Logger.getLogger(StreamGobbler.class);
public static final int INFO = 0;
public static final int ERROR = 1;
public StreamGobbler(InputStream is, int type) {
this.is = is;
this.type = type;
}
@Override
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
if (type == INFO) {
logger.info(line);
} else if (type == ERROR) {
logger.error(line);
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
捐助開發者
在興趣的驅動下,寫一個
免費
的東西,有欣喜,也還有汗水,希望你喜歡我的作品,同時也能支援一下。 當然,有錢捧個錢場(右上角的愛心标志,支援支付寶和PayPal捐助),沒錢捧個人場,謝謝各位。

謝謝您的贊助,我會做的更好!