Java运行cmd命令
2015-06-21 17:44
465 查看
public void apktool(){ apkFile = new File(apkFilePath); String debugFilePath = apkFile.getParent() + "\\debug"; debugFile = new File(debugFilePath); if(!debugFile.exists()){ debugFile.mkdir(); } String commanApktool = String.format("cmd /c apktool d -f %s -o %s", apkFilePath,debugFilePath); System.out.println(commanApktool); runCmd(commanApktool); }
<span style="color: rgb(84, 84, 84); font-family: Verdana, Arial; font-size: 14px; line-height: 18px;">cmd /c dir 是执行完dir命令后关闭命令窗口。 </span><br style="color: rgb(84, 84, 84); font-family: Verdana, Arial; font-size: 14px; line-height: 18px;" /><br style="color: rgb(84, 84, 84); font-family: Verdana, Arial; font-size: 14px; line-height: 18px;" /><span style="color: rgb(84, 84, 84); font-family: Verdana, Arial; font-size: 14px; line-height: 18px;">cmd /k dir 是执行完dir命令后不关闭命令窗口。 </span><br style="color: rgb(84, 84, 84); font-family: Verdana, Arial; font-size: 14px; line-height: 18px;" /><br style="color: rgb(84, 84, 84); font-family: Verdana, Arial; font-size: 14px; line-height: 18px;" /><span style="color: rgb(84, 84, 84); font-family: Verdana, Arial; font-size: 14px; line-height: 18px;">cmd /c start dir 会打开一个新窗口后执行dir指令,原窗口会关闭。 </span><br style="color: rgb(84, 84, 84); font-family: Verdana, Arial; font-size: 14px; line-height: 18px;" /><br style="color: rgb(84, 84, 84); font-family: Verdana, Arial; font-size: 14px; line-height: 18px;" /><span style="color: rgb(84, 84, 84); font-family: Verdana, Arial; font-size: 14px; line-height: 18px;">cmd /k start dir 会打开一个新窗口后执行dir指令,原窗口不会关闭。</span>
/** * 运行cmd命令 * @param cmd */ public void runCmd(String cmd) { Process p = null; try { p = Runtime.getRuntime().exec(cmd); //获取进程的正确流 final InputStream is1 = p.getInputStream(); //获取进程的错误流 final InputStream is2 = p.getErrorStream(); //处理正确的流信息 new Thread() { public void run() { BufferedReader br1 = new BufferedReader( new InputStreamReader(is1)); try { String line1 = null; while ((line1 = br1.readLine()) != null) { if (line1 != null) { } } } catch (IOException e) { e.printStackTrace(); } finally { try { is1.close(); } catch (IOException e) { e.printStackTrace(); } } } }.start(); //获取错误的流信息 new Thread() { public void run() { BufferedReader br2 = new BufferedReader( new InputStreamReader(is2)); try { String line2 = null; while ((line2 = br2.readLine()) != null) { if (line2 != null) { } } } catch (IOException e) { e.printStackTrace(); } finally { try { is2.close(); } catch (IOException e) { e.printStackTrace(); } } } }.start(); p.waitFor(); p.destroy(); } catch (Exception e) { try { p.getErrorStream().close(); p.getInputStream().close(); p.getOutputStream().close(); } catch (Exception ee) { ee.printStackTrace(); } } }
相关文章推荐
- spring配合ibatis操作数据库完整例子
- Java并发编程-36-原子数组
- Java--Reflect(反射)专题3——获取类的方法的所有信息
- spring 定时任务相关链接
- RegExp正则校验之Java及R测试
- Java反射
- JAVA+C笔记
- Java并发编程-35-原子变量-atomic
- Struts2 中路径问题
- JAVA 企业级框架、安全权限、高并发、多线程、稳定强
- 使用Spring MVC 4构建Restful服务 搭建Spring MVC 4开发环境八步走
- java读写数据
- ServletContext与ServletConfig的详解及差异
- JFrame重绘去掉默认的标题图标等
- Java Card CAP 文件组件分析
- Java卡环境介绍
- java中String.intern() 方法到底是干啥的?
- Google Java编程风格指南
- 第一章:Spring Data JPA入门
- Java IO 小结