Runtime.getRuntime().exec()方法主要用于运行外部的程序或命令。

Runtime.getRuntime().exec共同拥有六个重载方法:

1.public Process exec(String command)

在单独的进程中运行指定的字符串命令。

2.public Process exec(String [] cmdArray)

在单独的进程中运行指定命令和变量

3.public Process exec(String command, String [] envp)

在指定环境的独立进程中运行指定命令和变量

4.public Process exec(String [] cmdArray, String [] envp)

在指定环境的独立进程中运行指定的命令和变量

5.public Process exec(String command,String[] envp,File dir)

在有指定环境和工作文件夹的独立进程中运行指定的字符串命令

6.public Process exec(String[] cmdarray,String[] envp,File dir)

在指定环境和工作文件夹的独立进程中运行指定的命令和变量



我们先来比較exec(String command)与exec(String[] cmdArray)的差别。事实上他们是等价的。终于都会调用:

exec(String[] cmdarray,String[] envp,File dir),我们看看方法exec(String cmdarray,String[] envp,File dir) throws IOException的实现代码:

public Process exec(String command, String[] envp, File dir) throws IOException {
if (command.length() == 0) throw new IllegalArgumentException("Empty command");
StringTokenizer st = new StringTokenizer(command);
String[] cmdarray = new String[st.countTokens()];
for (int i = 0; st.hasMoreTokens(); i++)
cmdarray[i] = st.nextToken();
return exec(cmdarray, envp, dir);
}

从上面的代码,我们能够看出终于调用的代码都是:exec(String[] cmdArray,String envp,File  dir)。exec(String command)相当于exec(command,null,null),exec(String[] cmdArray)相当于exec(cmdArray,null,null)。

參数说明:

cmdarray - 包括所调用命令及其參数的数组。

envp - 字符串数组,当中每一个元素的环境变量的设置格式为 name=value,假设子进程应该继承当前进程的环境,或该參数为 null。

dir - 子进程的工作文件夹;假设子进程应该继承当前进程的工作文件夹。则该參数为 null。



另外,运行exec(String command)不等同于直接运行command line命令,比方命令:

javap -l xxx > output.txt

这时要用exec(String[] cmdArray)。如例:

Process p = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c",

"javap -l xxx > output.txt"});

关于返回结果类型:Process,它有几个方法:

1.destroy():杀掉子进程

2.exitValue():返回子进程的出口值,值 0 表示正常终止

3.getErrorStream():获取子进程的错误流

4.getInputStream():获取子进程的输入流

5.getOutputStream():获取子进程的输出流

6.waitFor():导致当前线程等待,如有必要,一直要等到由该 Process 对象表示的进程已经终止。假设已终止该子进程。此方法马上返回。假设没有终止该子进程,调用的线程将被堵塞。直到退出子进程,依据惯例,0 表示正常终止

对Runtime.getRuntime.exec()的了解能够參考博客:http://www.cnblogs.com/mingforyou/p/3551199.html

案列:实现Log中的日志显示(以对话框的形式演示,能够将输出日志保存到详细的文件里)

public class MainActivity extends Activity implements OnClickListener{
Button conLog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
conLog = (Button) findViewById(R.id.conLog);//通过id找到的button
MyLog.isDebug = true;//debug模式开启
conLog.setOnClickListener(this);
}
public void onClick(View v) {
//button被点击到了。收集收集日志
try {
readLog();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @author Danny QQ 858030348
* @throws IOException
*
*/
private void readLog() throws IOException {
MyLog.i("INFO", "start connectLog");
StringBuffer sb = new StringBuffer();
ArrayList<String> cmdLine = new ArrayList<String>();
cmdLine.add("logcat");
cmdLine.add("-d");//收集一次日志停止
cmdLine.add("-s");//过滤
cmdLine.add("INFO");
System.out.println(cmdLine.toArray(new String[cmdLine.size()]));
Process exec = Runtime.getRuntime().exec(cmdLine.toArray(new String[cmdLine.size()]));
//获取运行命令后的输入流
InputStream inputStream = exec.getInputStream();
InputStreamReader buInputStreamReader = new InputStreamReader(inputStream);//装饰器模式
BufferedReader bufferedReader = new BufferedReader(buInputStreamReader);//直接读字符串
String str = null;
while((str = bufferedReader.readLine())!=null){
sb.append(str);//每读一行拼接到sb里面去
sb.append("\n");//每一行一个换行符
}
//吐司
Toast.makeText(this, sb.toString(), 1000).show();
}
}

对于Logcat的命令能够參考博客:http://www.jb51.net/article/47055.htm

最新文章

  1. html canvas 弹球(模仿)
  2. VS2010的项目配置
  3. Java BigDecimal和double
  4. PriorityQueue
  5. R入门&lt;二&gt;-时间序列研究
  6. UFS
  7. Stream探究
  8. nyoj137 取石子(三) 楼教主男人八题之一
  9. 基于Windows,Python,Theano的深度学习框架Keras的配置
  10. 五、stdout,stdoin和stderr
  11. English 翻译到Vyeshal的软件
  12. vue处理异步数据踩过的坑
  13. windows 基础命令小集
  14. Spring日记_01 之 Eclipse下的Tomcat服务器配置 以及 Springmvc和Servlet的使用
  15. python 之 模块
  16. java类中根据已有的变量复写类的toString方法
  17. AutoMapper之集合和数组映射
  18. (F - 超级英雄Hero HYSBZ - 1191 )匈牙利算法
  19. webservice的SOAP代理设置
  20. 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 Sum

热门文章

  1. aplusb 数论
  2. Gerrit使用简介
  3. soap1.1与soap1.2区别
  4. java中的BitSet
  5. duilib入门简明教程 -- 第一个程序 Hello World(3) (转)
  6. MQ 分拆Json数据包然后上传
  7. ViewAnimator实现复杂的动画效果
  8. 洛谷 P2089 烤鸡【DFS递归/10重枚举】
  9. Linux笔记:vim
  10. 【IntelliJ IDEA】spring boot项目在idea实现自动部署