如果想要了解更多的信息,参阅代码里面给的链接
下面是这个正确的例子
- public class RuntimeExec {
-
-
-
-
-
-
-
-
-
public static boolean runtimeExec(String cmd) {
-
try {
-
Process proc = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", cmd});
-
-
-
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
-
-
-
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
-
-
- errorGobbler.start();
- outputGobbler.start();
-
-
-
if (proc.waitFor() != 0) {
-
System.err.println("执行\"" + cmd + "\"时返回值=" + proc.exitValue());
-
return false;
-
} else {
-
return true;
- }
-
} catch (Exception e) {
- e.printStackTrace();
-
return false;
- }
- }
-
-
static class StreamGobbler extends Thread {
- InputStream is;
- String type;
-
- StreamGobbler(InputStream is, String type) {
-
this.is = is;
-
this.type = type;
- }
-
-
public void run() {
-
try {
-
InputStreamReader isr = new InputStreamReader(is);
-
BufferedReader br = new BufferedReader(isr);
-
String line = null;
-
while ((line = br.readLine()) != null)
-
System.out.println(type + ">" + line);
-
} catch (IOException ioe) {
- ioe.printStackTrace();
- }
- }
- }
-
- }
分享到:
相关推荐
1,申请root权限Runtime.getRuntime().exec("su"); 2,通过数据输出流DataOutputStream写入pm install命令; 3,最后获取Process进程的返回值int i = process.waitFor();,如果i=0,则表明已获取root权限。
完美解决runtime.exec()执行进程block死锁以及为waitFor设置超时 不需要耗cpu的循环判断exitValue==0 开两个进程搞定
Runtime 执行bat
windows环境下IDEA java代码Runtime.getRuntime.exec中shell的执行环境的解决方案前言解决办法后记 前言 在使用IDEA本地开发监控守护线程的后台,我遇上了执行环境不兼容的问题,爆出各种“xxx不是内部或外部命令,...
这里不是通过view来截图,也不是通过底层的framebuffer实现截图,而是采用另外一种方法实现截图,通过Runtime.getRuntime().exec()来实现,并保存在sdcard上,代码很简单。
Runtime runtime = Runtime.getRuntime(); try { runtime.exec("cmd /c start " + url); } catch (IOException e) { e.printStackTrace(); } } /** * 鍦ㄥ欢杩熸寚瀹氱殑绉掓暟鍚庡叧鏈? * ...
//Process p = Runtime.getRuntime().exec("su"); //然后,在向这个进程的写入要执行的命令,即可达到以root权限执行命令: //dos.flush(); //或者用下面的方式: //Runtime.getRuntime().exec&#...
(注意:Runtime.getRuntime().exec(command)返回的是一个Process类的实例), 该实例可用于控制进程或取得进程的相关信息. 由于调用Runtime.exec方法所创建的子进程没有自己的终端或控制台,因此该子进程的标准IO...
主要介绍了Java编程使用Runtime和Process类运行外部程序的方法,结合实例形式分析了java使用Runtime.getRuntime().exec()方法运行外部程序的常见情况与操作技巧,需要的朋友可以参考下
动态权限工具类
java的Runtime.getRuntime().exec(commandStr)可以调用执行cmd指令。 cmd /c dir 是执行完dir命令后关闭命令窗口。 cmd /k dir 是执行完dir命令后不关闭命令窗口。 cmd /c start dir 会打开一个新...
利用Runtime call操作系统的命令,具体的命令取决于不同的操作系统,注意不要调用Runtime.getRuntime().exec(String)接口,要用Runtime.getRuntime().exec(String[])这个接口,不然复杂命令的执行会有问题。...
直接看代码:方法一: 代码如下:Runtime.getRuntime().exec(“rundll32 url.dll,FileProtocolHandler //www.jb51.net”); 方法二: 代码如下://判断当前系统是否支持Java AWT Desktop扩展 if(java.awt....
java用Runtime.getRuntime().exec(cmd)调用js即可,
所以我们必须到Dos环境下去设置,在java中用Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath()+ """+ " +R")该方法可以实现。因为路径file.getAbsolutePath()中可能会还有空格,所以必须...
Runtime.getRuntime().exec("sh")以及linux echo写入指令
java 查看任务管理里面的所有线程 Proces java.lang.Runtime.getRuntime().exec("ipconfig");
从公共下载: docker pull dockerfile/python-runtime (或者,您可以从 Dockerfile 构建映像: docker docker build -t="dockerfile/python-runtime" github.com/dockerfile/python-runtime ) 用法 此图像假定您...
蜂鸣器exe,可用java调用 Runtime.getRuntime().exec("d:\\beep.exe");