0. Runtime.exec()用来执行外部程序或命令
1. Runtime.exec() 有四种调用方法
* public Process exec(String command);
* public Process exec(String [] cmdArray);
* public Process exec(String command, String [] envp);
* public Process exec(String [] cmdArray, String [] envp);
2. 得到程序执行返回值, 0为success
需要用waitFor()函数,比如
Process p = Runtime.getRuntime().exec("javac");
(处理.....)
int exitVal = p.waitFor();
3. 得到程序执行的结果或错误信息
需要用BufferedInputStream 和 BufferReader来得到,否则程序会hang
比如得到错误信息用p.getErrorStream(),然后输出即可:
BufferedInputStream in = new BufferedInputStream(p.getErrorStream());
BufferedReader br = new BufferedReader(new InputStreamReader(in));
4. Runtime.exec() 不等同于直接执行command line命令!
啊,我算是在这里吃了苦头了。Runtime.exec()很有局限性,对有些命令不能直接把command line里的内容当作String参数传给exec().
比如重定向等命令。举个例子:
javap -l xxx > output.txt
这时要用到exec的第二种重载,即input 参数为String[]:
Process p = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c","javap -l xxx > output.txt"});
转自:http://blog.csdn.net/flying881114/article/details/6272472
分享到:
相关推荐
完美解决runtime.exec()执行进程block死锁以及为waitFor设置超时 不需要耗cpu的循环判断exitValue==0 开两个进程搞定
将runtime.exec()的四个陷阱用中文描述
NULL 博文链接:https://jisonami.iteye.com/blog/2181810
java 执行可执行文件,Runtime.exec、ProcessBuilder、commons-exec
System.Runtime.Serialization.dll System.Runtime.Serialization.dll System.Runtime.Serialization.dll
System.Runtime.Serialization.DLL
System.Runtime.InteropServices.APIs.xml
在本篇文章里小编给大家整理的是一篇关于java.lang.Runtime.exec() Payload知识点相关内容,有兴趣的朋友们学习下。
1,申请root权限Runtime.getRuntime().exec("su"); 2,通过数据输出流DataOutputStream写入pm install命令; 3,最后获取Process进程的返回值int i = process.waitFor();,如果i=0,则表明已获取root权限。
System.Runtime.InteropServices.APIs.dll
Microsoft.NET.Native.Runtime.2.2_2.2.28604.0_x64__8wekyb3d8bbwe
内容:vue.js、vue.min.js、vue.common.js、vue.common.dev.js、vue.common.prod.js、vue.esm.browser.js、vue.esm.browser.min.js、vue.runtime.js、vue.runtime.min.js、vue.runtime.esm.js、vue.runtime.common....
windows环境下IDEA java代码Runtime.getRuntime.exec中shell的执行环境的解决方案前言解决办法后记 前言 在使用IDEA本地开发监控守护线程的后台,我遇上了执行环境不兼容的问题,爆出各种“xxx不是内部或外部命令,...
ArcGIS Runtime SDK for Java 100.12.0 的...如需在生产环境中使用ArcGIS Runtime SDK for Java,请联系ESRI公司获得授权,现在也有国产替代产品GeoScne Runtime SDK for Java,可以联系易智瑞公司获得相关资源和授权。
Esri.ArcGISRuntime.100.3.0.nupkg Esri.ArcGISRuntime.LocalServices.100.3.0.nupkg Esri.ArcGISRuntime.Hydrography.100.3.0.nupkg Esri.ArcGISRuntime.UWP.100.3.0.nupkg Esri.ArcGISRuntime.Xamarin.Forms.100.3...
解决 C# 4.5 使用MongoDB遇到的问题 错误:缺少 System.Runtime.InteropServices.RuntimeInformation.dll version = 4.0.0
org.eclipse.core.runtime.compatibility.auth依赖jar,解决svn某种情况下不能保存密码的问题
Java Runtime Environment 1.6.0.7 (32-bit