package com.as.common; import java.io.DataOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; public class ShellUtils { private ShellUtils() { throw new AssertionError(); } public static CommandResult execCommand(String command) { return execCommand(new String[]{command}, ".", true); } public static int execCommand(String[] commands, String path) { return execCommand(commands, path, false).result; } public static CommandResult execCommand(String[] commands, String path, boolean isNeedResultMsg) { int result = -1; if (commands == null || commands.length == 0) { return new CommandResult(result, null, null); } Process process = null; InputStream successResult = null; InputStream errorResult = null; StringBuilder successMsg = null; StringBuilder errorMsg = null; DataOutputStream os = null; try { // process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH); process = Runtime.getRuntime().exec(commands, null, new File(path)); // os = new DataOutputStream(process.getOutputStream()); // for (String command : commands) { // if (command == null) { // continue; // } // // donnot use os.writeBytes(commmand), avoid chinese charset error // os.write(command.getBytes()); // os.writeBytes(COMMAND_LINE_END); // os.flush(); // } // os.writeBytes(COMMAND_EXIT); // os.flush(); result = process.waitFor(); // get command result if (isNeedResultMsg) { successMsg = new StringBuilder(); errorMsg = new StringBuilder(); successResult = process.getInputStream(); errorResult = process.getErrorStream(); byte[] buf = new byte[1024]; for (int n; (n = successResult.read(buf)) != -1; ) { successMsg.append(new String(buf, 0, n)); } for (int n; (n = errorResult.read(buf)) != -1; ) { errorMsg.append(new String(buf, 0, n)); } } } catch (Exception e) { e.printStackTrace(); } finally { try { if (os != null) { os.close(); } if (successResult != null) { successResult.close(); } if (errorResult != null) { errorResult.close(); } } catch (IOException e) { e.printStackTrace(); } if (process != null) { process.destroy(); } } return new CommandResult(result, successMsg == null ? null : successMsg.toString(), errorMsg == null ? null : errorMsg.toString()); } /** * result of command * <ul> * <li>{@link CommandResult#result} means result of command, 0 means normal, else means error, same to excute in * linux shell</li> * <li>{@link CommandResult#successMsg} means success message of command result</li> * <li>{@link CommandResult#errorMsg} means error message of command result</li> * </ul> * * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2013-5-16 */ @SuppressWarnings("unused") public static class CommandResult { /** * result of command **/ public int result; /** * success message of command result **/ public String successMsg; /** * error message of command result **/ public String errorMsg; public CommandResult(int result) { this.result = result; } public CommandResult(int result, String successMsg, String errorMsg) { this.result = result; this.successMsg = successMsg; this.errorMsg = errorMsg; } } }
最近下载更多