使用java執行命令簡易封裝類
在java中有時我們會調用系統命令或批處理或shell腳本
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader;import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory;
public abstract class ExecCommand { private static final Log log = LogFactory.getLog(ExecCommand.class);
/** * * @描述 在單獨的進程中執行指定的字符串命令。 * @作者 鐘誠 * @日期 Sep 9, 2011 * @時間 5:15:48 PM * @param command 一條指定的系統命令 * @throws IOException */ public void exec(String command) throws IOException { exec(command , null , null); } /** * * @描述 在有指定工作目錄的獨立進程中執行指定的字符串命令。 * @作者 鐘誠 * @日期 Sep 17, 2011 * @時間 11:17:59 AM * @param command 一條指定的系統命令 * @param workpath 子進程的工作目錄;如果子進程應該繼承當前進程的工作目錄,則該參數為null。 * @throws IOException */ public void exec(String command,String workpath) throws IOException { exec(command , null , workpath); } /** * * @描述 在有指定環境和工作目錄的獨立進程中執行指定的字符串命令。 * @作者 鐘誠 * @日期 Sep 17, 2011 * @時間 11:21:28 AM * @param command 一條指定的系統命令 * @param envp 環境變量,字符串數組,其中每個元素的環境變量設置格式為 name=value;如果子進程應該繼承當前進程的環境,則為null。 * @param path 子進程的工作目錄;如果子進程應該繼承當前進程的工作目錄,則該參數為null。 * @throws IOException */ public void exec(String command,String[] envp,String workpath) throws IOException { InputStream is = null; BufferedInputStream in = null; BufferedReader br = null; try { File dir=null; if(null != workpath) dir=new File(workpath); log.info("【COMMAND】>>> "+command); // InputStream is = Runtime.getRuntime().exec(new String[]{"ping","127.0.0.1"}).getInputStream(); is = Runtime.getRuntime().exec(command,envp,dir).getInputStream(); in = new BufferedInputStream(is); br = new BufferedReader(new InputStreamReader(in)); String ss = ""; while ((ss = br.readLine()) != null) { lineHandler(ss); } } finally { if (null != br) br.close(); if (null != in) in.close(); if (null != is) is.close(); } } /** * * @描述 在單獨的進程中執行指定的命令和參數。 * @作者 鐘誠 * @日期 Sep 9, 2011 * @時間 5:15:48 PM * @param commands 包含所調用命令及其參數的數組。例如:new String[]{"/home/user1/test.sh","arg1","arg2"}; * @throws IOException */ public void exec(String[] commands) throws IOException { exec(commands , null , null); } /** * * @描述 在有指定工作目錄的獨立進程中執行指定的字符串命令。 * @作者 鐘誠 * @日期 Sep 17, 2011 * @時間 11:17:59 AM * @param commands 包含所調用命令及其參數的數組。例如:new String[]{"/home/user1/test.sh","arg1","arg2"}; * @param workpath 子進程的工作目錄;如果子進程應該繼承當前進程的工作目錄,則該參數為null。 * @throws IOException */ public void exec(String[] commands,String workpath) throws IOException { exec(commands , null , workpath); } /** * * @描述 在有指定環境和工作目錄的獨立進程中執行指定的字符串命令。 * @作者 鐘誠 * @日期 Sep 9, 2011 * @時間 5:18:00 PM * @param commands 包含所調用命令及其參數的數組。例如:new String[]{"/home/user1/test.sh","arg1","arg2"}; * @param envp 環境變量,字符串數組,其中每個元素的環境變量設置格式為 name=value;如果子進程應該繼承當前進程的環境,則為null。 * @param path 子進程的工作目錄;如果子進程應該繼承當前進程的工作目錄,則該參數為null。 * @throws IOException */ public void exec(String[] commands,String[] envp , String workpath) throws IOException { InputStream is = null; BufferedInputStream in = null; BufferedReader br = null; try { File dir=null; if(null != workpath) dir=new File(workpath); log.info("【COMMAND】>>>:"+getCommandString(commands)); is = Runtime.getRuntime().exec(commands,envp,dir).getInputStream(); in = new BufferedInputStream(is); br = new BufferedReader(new InputStreamReader(in)); String ss = ""; while ((ss = br.readLine()) != null) { lineHandler(ss); } } finally { if (null != br) br.close(); if (null != in) in.close(); if (null != is) is.close(); } } /** * * @描述 僅為日志輸出,無其他作用 * @作者 鐘誠 * @日期 Sep 13, 2011 * @時間 1:48:06 PM * @param commands * @return */ private String getCommandString(String[] commands){ StringBuffer sb=new StringBuffer(); for(String command:commands){ sb.append(command); sb.append(" "); } return sb.toString(); } /** * * @描述 行處理 * @作者 鐘誠 * @日期 Sep 9, 2011 * @時間 5:22:11 PM * @param lineStr */ protected abstract void lineHandler(String lineStr);
} </pre>
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory;public class ExecuteCommand extends ExecCommand { private static final Log log = LogFactory.getLog(ExecuteCommand.class);
/** * @描述 執行命令返回行內容處理默認直接輸出到日志<br /> * 如果需要自定義處理請覆蓋此方法。 * @作者 鐘誠 * @時間 2011-09-09 17:15 */ @Override protected void lineHandler(String lineStr) { log.info(lineStr); }
} </pre>
public class ExecCommandTest { public static void main(String[] args) throws Exception { // 默認處理 ExecCommand exe1 = new ExecuteCommand(); exe1.exec("c:\\ls.bat"); // 自定義處理 ExecCommand exe2 = new ExecuteCommand() { @Override protected void lineHandler(String lineStr) { System.out.println(lineStr); } }; exe2.exec("c:\\ls.bat","c:\\"); // 多個參數 ExecCommand exe3 = new ExecuteCommand(); exe3.exec(new String[] { "ping", "127.0.0.1" }); } }
本文由用戶 dd2d 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!