Runtime.getRuntime().exec(...)使用方法

如果想要了解更多的信息,参阅代码里面给的链接 



下面是这个正确的例子

  1. public class RuntimeExec {
  2. /**
  3. * Runtime execute.
  4. *
  5. * @param cmd the command.
  6. * @return success or failure
  7. * @see {@link http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=4}
  8. * @since 1.1
  9. */
  10. public static boolean runtimeExec(String cmd) {
  11. try {
  12. Process proc = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", cmd});
  13. // any error message?
  14. StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
  15. // any output?
  16. StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
  17. ) {
  18. System.err.println("执行\"" + cmd + "\"时返回值=" + proc.exitValue());
  19. return false;
  20. else {
  21. return true;
  22. }
  23. catch (Exception e) {
  24. e.printStackTrace();
  25. return false;
  26. }
  27. }
  28. static class StreamGobbler extends Thread {
  29. InputStream is;
  30. String type;
  31. StreamGobbler(InputStream is, String type) {
  32. this.is = is;
  33. this.type = type;
  34. }
  35. public void run() {
  36. try {
  37. InputStreamReader isr = new InputStreamReader(is);
  38. BufferedReader br = new BufferedReader(isr);
  39. String line = null;
  40. while ((line = br.readLine()) != null)
  41. System.out.println(type + ">" + line);
  42. catch (IOException ioe) {
  43. ioe.printStackTrace();
  44. }
  45. }
  46. }
  47. }

Runtime.getRuntime().exec(...)使用方法的更多相关文章

  1. 关于Runtime.getRuntime().exec()产生阻塞的2个陷阱

    本文来自网易云社区 背景 相信做java服务端开发的童鞋,经常会遇到Java应用调用外部命令启动一些新进程来执行一些操作的场景,这时候就会使用到Runtime.getRuntime().exec(), ...

  2. 使用Runtime.getRuntime().exec()方法的几个陷阱 (转)

    Process 子类的一个实例,该实例可用来控制进程并获得相关信息.Process 类提供了执行从进程输入.执行输出到进程.等待进程完成.检查进程的退出状态以及销毁(杀掉)进程的方法. 创建进程的方法 ...

  3. [转]使用Runtime.getRuntime().exec()方法的几个陷阱

    Process 子类的一个实例,该实例可用来控制进程并获得相关信息.Process 类提供了执行从进程输入.执行输出到进程.等待进程完成.检查进程的退出状态以及销毁(杀掉)进程的方法. 创建进程的方法 ...

  4. 使用Runtime.getRuntime().exec()方法的几个陷阱

    Process 子类的一个实例,该实例可用来控制进程并获得相关信息.Process 类提供了执行从进程输入.执行输出到进程.等待进程完成.检查进程的退出状态以及销毁(杀掉)进程的方法. 创建进程的方法 ...

  5. Runtime.getRuntime().exec方法

    Runtime.getRuntime().exec()方法主要用于执行外部的程序或命令. Runtime.getRuntime().exec共有六个重载方法: public Process exec( ...

  6. 用Runtime.getRuntime().exec()需要注意的地方

    有时候我们可能需要调用系统外部的某个程序,此时就可以用Runtime.getRuntime().exec()来调用,他会生成一个新的进程去运行调用的程序. 此方法返回一个java.lang.Proce ...

  7. Runtime.getRuntime().exec中命令含有括号问题

    在写批量运行bat工具的时候.想起了之前写的定时小工具里面的运行方法. 使用Runtime.getRuntime().exec方法. Runtime.getRuntime().exec("c ...

  8. [转]java调用外部程序Runtime.getRuntime().exec

    Runtime.getRuntime().exec()方法主要用于执行外部的程序或命令. Runtime.getRuntime().exec共有六个重载方法: public Process exec( ...

  9. Runtime.getRuntime.exec();

    杀死Chrome浏览器进程 private static void closeAllChrome() throws IOException{ Runtime.getRuntime().exec(&qu ...

随机推荐

  1. 龟兔赛跑(DP)

    龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  2. 普通IT和文艺IT工程师的区别

    在一个UITableView的editing设置的方法实现过程中,我想到两种写法,顺便想了一下两种方法的区别.觉得这时一个普通IT工程师和NB工程师的区别一个有趣的印记. 您通常时怎么去实现的呢? - ...

  3. Hibernate中,left join、inner join以及left join fetch区别(转)

    标签: hibernate hql inner join left right 杂谈 分类: SQL 原文地址:http://m33707.iteye.com/blog/829725 Select F ...

  4. 如何避免JSP乱码

    如何解决JavaWeb乱码问题   作为一个合格的web开发人员应该是什么问题都遇到过的,尤其是乱码问题.大家也许都体会到了,我们中国人学编程,很大的一个不便就是程序的编码问题,无论学习什么技术,我们 ...

  5. ImageMagick还是GraphicsMagick?

    引自:http://co63oc.blog.51cto.com/904636/328997 ImageMagick(IM) 套装包含的命令行图形工具是一主要自由软件:Linux,其他类Unix操作系统 ...

  6. 转:详细解说 STL 排序(Sort)

    详细解说 STL 排序(Sort) 详细解说 STL 排序(Sort) 作者Winter 详细解说 STL 排序(Sort) 0 前言: STL,为什么你必须掌握 1 STL提供的Sort 算法 1. ...

  7. lwp 模拟行锁堵塞 前端超时

    jrhmpt01:/root/async# cat a2.pl use LWP::UserAgent; use utf8; use DBI; use POSIX; use HTTP::Date qw( ...

  8. 在Android Studio中使用Gradle方便地修改包名

    情景: 主Module引用了多个module,在代码使用R.xx.xx的时候,会import 当前包名.R.而由于需要上架Play做测试,可是目前的包名已经被使用了,所以需要修改包名. 正确使用bui ...

  9. 【翻译】探究Ext JS 5和Sencha Touch的布局系统

    原文:Exploring the Layout System in Ext JS 5 and Sencha Touch 布局系统是Sencha框架中最强大和最有特色的一个部分. 布局要处理应用程序中每 ...

  10. YUV / RGB 格式及快速转换算法

    1 前言 自然界的颜色千变万化,为了给颜色一个量化的衡量标准,就需要建立色彩空间模型来描述各种各样的颜色,由于人对色彩的感知是一个复杂的生理和心理联合作用 的过程,所以在不同的应用领域中为了更好更准确 ...