java shell命令工具类
2016-03-09 17:04
447 查看
import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.List; /** java shell命令工具类 by yunshouhu javac JavaShellUtil.java -encoding utf-8 */ public class JavaShellUtil { public static String lineseparator=System.getProperty("line.separator"); public static String COMMAND_SH = "sh"; public static String COMMAND_EXIT = "exit\n"; public static String COMMAND_LINE_END = "\n"; static { if(System.getProperty("os.name").toUpperCase().indexOf("WINDOWS")!=-1) { System.out.println("window"); COMMAND_SH="cmd"; }else{ System.out.println("unix"); } } public static void main(String[] args) { System.out.println(JavaShellUtil.execCommand("dir").toString()); System.out.println(JavaShellUtil.execCommand("ls -l").toString()); //System.out.println(JavaShellUtil.execCommand("ping www.baidu.com").toString()); System.out.println(JavaShellUtil.execCommand("aapt v").toString()); System.out.println(JavaShellUtil.execCommand("aapt.exe").toString()); } public static CommandResult execCommand(String command) { return execCommand(new String[] {command}, true); } public static CommandResult execCommand(String command, boolean isNeedResultMsg) { return execCommand(new String[]{command}, isNeedResultMsg); } public static CommandResult execCommand(List<String> commands, boolean isNeedResultMsg) { return execCommand(commands == null ? null : commands.toArray(new String[]{}), isNeedResultMsg); } /** * execute shell commands * {@link CommandResult#result} is -1, there maybe some excepiton. * * @param commands command array * @param needResponse whether need result msg */ public static CommandResult execCommand(String[] commands, final boolean needResponse) { int result = -1; if (commands == null || commands.length == 0) { return new CommandResult(result, null, "空命令"); } Process process = null; final StringBuilder successMsg = new StringBuilder(); final StringBuilder errorMsg = new StringBuilder(); DataOutputStream os = null; try { process = Runtime.getRuntime().exec(COMMAND_SH); 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(); final BufferedReader successResult = new BufferedReader(new InputStreamReader(process.getInputStream())); final BufferedReader errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream())); //http://249wangmang.blog.163.com/blog/static/52630765201261334351635/ new Thread(new Runnable() { public void run() { try { if (needResponse) { String s; while ((s = successResult.readLine()) != null) { successMsg.append(s); successMsg.append(lineseparator); } } } catch (IOException e) { e.printStackTrace(); } } }).start(); //启动两个线程,解决process.waitFor()阻塞问题 new Thread(new Runnable() { public void run() { try { if (needResponse) { String s; while ((s = errorResult.readLine()) != null) { errorMsg.append(s); errorMsg.append(lineseparator); } } } catch (IOException e) { e.printStackTrace(); } } }).start(); result = process.waitFor(); if (errorResult != null) { errorResult.close(); } if (successResult != null) { successResult.close(); } } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (os != null) { os.close(); } } catch (IOException e) { e.printStackTrace(); } finally { if (process != null) { process.destroy(); } } } return new CommandResult(result, successMsg == null ? null : successMsg.toString(), errorMsg == null ? null : errorMsg.toString()); } public static class CommandResult { public int result; public String responseMsg; public String errorMsg; public CommandResult(int result) { this.result = result; } public CommandResult(int result, String responseMsg, String errorMsg) { this.result = result; this.responseMsg = responseMsg; this.errorMsg = errorMsg; } @Override public String toString() { return "CommandResult{" + "errorMsg='" + errorMsg + '\'' + ", result=" + result + ", responseMsg='" + responseMsg + '\'' + '}'; } } }
相关文章推荐
- Linux远程执行shell命令
- shell 使用中碰到的问题及解决方法
- shell(1):shell基础及函数,字符串,if,while,时间处理等应用
- linux中shell变量$#,$@,$0,$1,$2的含义解释
- shell中的括号(小括号,中括号,大括号)及单引号、 双引号,反引号(``)
- linux中shell变量$#,$@,$0,$1,$2的含义解释
- shell脚本调试运行
- shell
- shell笔记
- secureCRT 从服务器上 下载或上传文件
- shell脚本学习
- Linux学习笔记(八)--shell编程(下)
- shell 学习记录(1)
- shell中for循环
- shell变量
- Shell基础(二):Bash变量
- shell编程(三):监控信息、进程管理
- shell:读取文件的每一行内容并输出
- shell脚本抽取文本文件中指定字符串的方法:sed+grep方法、awk+grep方法(必要时可以联合sed以及grep)、grep+cut方法
- shell