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. 毕业bg(dfs)

    毕业bg Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissi ...

  2. IWebBrowser隐藏滚动条

    刚才在项目里看到一个IWebBrowser2,竟然需要通过MoveWindow的方式把滚动条遮挡,如果要缩小IWebBrowser2控件的显示大小呢?这种方法至少我用不习惯,起码也得从源头解决这样的问 ...

  3. gcc 的编译过程

    通常我们都是使用下面的命令来直接生成可执行文件 gcc demo.c -o demo 对于我们来说十分简单,但是对编译器来说却完成了一系列复杂的工作,概括起来有如下几步: 1. 预处理 gcc -E ...

  4. (iOS)viewController背景透明化

    #ifdef __IPHONE_8_0 ) { [UIApplication sharedApplication].keyWindow.rootViewController.providesPrese ...

  5. jQuery validate和form插件配套使用

    参考 官网http://jqueryvalidation.org/documentation/ 博客http://www.cnblogs.com/buzzlight/archive/2010/06/3 ...

  6. Wmic-linux

    Description Windows Management Instrumentation Command-line (WMIC) uses Windows Management Instrumen ...

  7. 如何修改Protel99SE原理图的标题栏

    本文主要讲述了如何修改Protel99SE原理图中的标题栏内容,使用者可以根据需要修改. 标题栏的格式: 1.添加模板:(1)菜单栏Design\Template\Set Template File ...

  8. 使用 Windows Media Center 远程控制

    http://windows.microsoft.com/en-us/windows/getting-started-windows-media-center#getting-started-wind ...

  9. C#使用WinAPI 修改电源设置,临时禁止笔记本合上盖子时睡眠

    原文 http://www.cnblogs.com/h46incon/archive/2013/09/03/3299138.html 在 阻止系统自动睡眠的小软件,附C#制作过程 ,弄了一个防止系统睡 ...

  10. HDU 3307 Description has only two Sentences

    数学实在是差到不行了…… #include <cstdio> #include <cstring> #include <algorithm> #include &l ...