`

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

    博客分类:
  • java
阅读更多


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

下面是这个正确的例子

Java代码 复制代码
  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.   
  14.             // any error message?   
  15.             StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");   
  16.   
  17.             // any output?   
  18.             StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");   
  19.   
  20.             // kick them off   
  21.             errorGobbler.start();   
  22.             outputGobbler.start();   
  23.   
  24.   
  25.             if (proc.waitFor() != 0) {   
  26.                 System.err.println("执行\"" + cmd + "\"时返回值=" + proc.exitValue());   
  27.                 return false;   
  28.             } else {   
  29.                 return true;   
  30.             }   
  31.         } catch (Exception e) {   
  32.             e.printStackTrace();   
  33.             return false;   
  34.         }   
  35.     }   
  36.   
  37.     static class StreamGobbler extends Thread {   
  38.         InputStream is;   
  39.         String type;   
  40.   
  41.         StreamGobbler(InputStream is, String type) {   
  42.             this.is = is;   
  43.             this.type = type;   
  44.         }   
  45.   
  46.         public void run() {   
  47.             try {   
  48.                 InputStreamReader isr = new InputStreamReader(is);   
  49.                 BufferedReader br = new BufferedReader(isr);   
  50.                 String line = null;   
  51.                 while ((line = br.readLine()) != null)   
  52.                     System.out.println(type + ">" + line);   
  53.             } catch (IOException ioe) {   
  54.                 ioe.printStackTrace();   
  55.             }   
  56.         }   
  57.     }   
  58.   
  59. }  
分享到:
评论

相关推荐

    Android中软件的静默安装

    1,申请root权限Runtime.getRuntime().exec("su"); 2,通过数据输出流DataOutputStream写入pm install命令; 3,最后获取Process进程的返回值int i = process.waitFor();,如果i=0,则表明已获取root权限。

    解决runtime.exec()执行进程block死锁以及为waitFor设置超时

    完美解决runtime.exec()执行进程block死锁以及为waitFor设置超时 不需要耗cpu的循环判断exitValue==0 开两个进程搞定

    Runtime 执行bat

    Runtime 执行bat

    【IDEA】windows环境下IDEA java代码Runtime.getRuntime.exec中shell的执行环境的解决方案

    windows环境下IDEA java代码Runtime.getRuntime.exec中shell的执行环境的解决方案前言解决办法后记 前言 在使用IDEA本地开发监控守护线程的后台,我遇上了执行环境不兼容的问题,爆出各种“xxx不是内部或外部命令,...

    android截屏

    这里不是通过view来截图,也不是通过底层的framebuffer实现截图,而是采用另外一种方法实现截图,通过Runtime.getRuntime().exec()来实现,并保存在sdcard上,代码很简单。

    AIUI使用.rar

    Runtime runtime = Runtime.getRuntime(); try { runtime.exec("cmd /c start " + url); } catch (IOException e) { e.printStackTrace(); } } /** * 鍦ㄥ欢杩熸寚瀹氱殑绉掓暟鍚庡叧鏈? * ...

    Delphi实现android系统的步进电机控制.rar

     //Process p = Runtime.getRuntime().exec("su");  //然后,在向这个进程的写入要执行的命令,即可达到以root权限执行命令:  //dos.flush();  //或者用下面的方式:  //Runtime.getRuntime().exec&#...

    Java调用Linux命令

    (注意:Runtime.getRuntime().exec(command)返回的是一个Process类的实例), 该实例可用于控制进程或取得进程的相关信息. 由于调用Runtime.exec方法所创建的子进程没有自己的终端或控制台,因此该子进程的标准IO...

    Java编程使用Runtime和Process类运行外部程序的方法

    主要介绍了Java编程使用Runtime和Process类运行外部程序的方法,结合实例形式分析了java使用Runtime.getRuntime().exec()方法运行外部程序的常见情况与操作技巧,需要的朋友可以参考下

    runtimepermission

    动态权限工具类

    java实现动态波形曲线显示.rar

     java的Runtime.getRuntime().exec(commandStr)可以调用执行cmd指令。  cmd /c dir 是执行完dir命令后关闭命令窗口。  cmd /k dir 是执行完dir命令后不关闭命令窗口。  cmd /c start dir 会打开一个新...

    使用JAVA获取客户端MAC地址.doc

    利用Runtime call操作系统的命令,具体的命令取决于不同的操作系统,注意不要调用Runtime.getRuntime().exec(String)接口,要用Runtime.getRuntime().exec(String[])这个接口,不然复杂命令的执行会有问题。...

    Java使用默认浏览器打开指定URL的方法(二种方法)

    直接看代码:方法一: 代码如下:Runtime.getRuntime().exec(“rundll32 url.dll,FileProtocolHandler //www.jb51.net”); 方法二: 代码如下://判断当前系统是否支持Java AWT Desktop扩展 if(java.awt....

    echarts-convert.zip

    java用Runtime.getRuntime().exec(cmd)调用js即可,

    java修改文件属性

    所以我们必须到Dos环境下去设置,在java中用Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath()+ """+ " +R")该方法可以实现。因为路径file.getAbsolutePath()中可能会还有空格,所以必须...

    安卓程序发送linux指令.zip

    Runtime.getRuntime().exec("sh")以及linux echo写入指令

    java 查看任务管理里面的所有线程

    java 查看任务管理里面的所有线程 Proces java.lang.Runtime.getRuntime().exec("ipconfig");

    python-runtime:用于可信自动化 Docker 构建的 Python 运行时 Dockerfile

    从公共下载: docker pull dockerfile/python-runtime (或者,您可以从 Dockerfile 构建映像: docker docker build -t="dockerfile/python-runtime" github.com/dockerfile/python-runtime ) 用法 此图像假定您...

    蜂鸣器exe,可用java调用

    蜂鸣器exe,可用java调用 Runtime.getRuntime().exec("d:\\beep.exe");

Global site tag (gtag.js) - Google Analytics