runtime.exec()
dir 指定了新子进程的工作目录。如果 dir 为 null,那么子进程会继承当前进程的当前工作目录
waitFor导致当前线程等待,如有必要,一直要等到由该 Process 对象表示的进程已经终止。如果已终止该子进程,此方法立即返回。如果没有终止该子进程,调用的线程将被阻塞,直到退出子进程,0 表示正常终止。
一些平台只为标准输入输出提供有限的缓存。错误的写子进程的输入流或者错误的都子进程的输出流都有可能造成子进程的阻塞,甚至是死锁。
getInputStream 得到正常信息
Runtime.exec() 不等同于直接执行command line命令!
Runtime.exec()很有局限性,对有些命令不能直接把command line里的内容当作String参数传给exec().
比如重定向等命令。举个例子:
javap -l xxx output.txt
这时要用到exec的第二种重载
把整个命令都当成/bin/sh的参数传入
linux下
Process p = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c","javap -l xxx output.txt"});
windows下
Process p = Runtime.getRuntime().exec(new String[]{"cmd","/c","javap -l xxx output.txt"});
linux执行shell脚本(带参数)
1
假设有一个shell脚本文件test.sh,有两个参数parm1,parm2,java调用的方法如下:
String[] cmd = {"/bin/sh","-c","test.sh parm1 parm2"};
Runtime.getRuntime().exec(cmd);
2.
不带参数
String PATH = "/XXXXX/a.sh"; try { Process num = Runtime.getRuntime().exec(PATH); } catch (IOException e) { e.printStackTrace(); }
带参数传入数组
String[] PATH = {"/XXXXX/a.sh","param1","param2"}; try { Process num = Runtime.getRuntime().exec(PATH); } catch (IOException e) { e.printStackTrace(); }windows下代码(需转码)
package com.zxing.imgQRCode; import java.io.BufferedReader; import java.io.InputStreamReader;linux下代码
/* Process p = Runtime.getRuntime().exec( "D:/Program Files/Wireshark/Wireshark.exe");*/ Process p = Runtime.getRuntime().exec( new String[]{"cmd","/c","dir"}); p.waitFor(); InputStreamReader in = new InputStreamReader( p.getInputStream(),"GBK"); BufferedReader br = new BufferedReader(in); String lineStr; while ((lineStr = br.readLine()) != null) { System.out.println(lineStr); } } catch (Exception e) { e.printStackTrace(); } } }
package com.zxing.imgQRCode; import java.io.BufferedReader; import java.io.InputStreamReader;Java执行带重定向或管道的shell命令的问题
Process p = Runtime.getRuntime().exec( new String[] { "/bin/sh", "-c", "ls test.txt"}, null, null); /*Process p = Runtime.getRuntime().exec( "D:/Program Files/Wireshark/Wireshark.exe");*/ Process p = Runtime.getRuntime().exec( new String[]{"cmd","/c","dir"});*/ p.waitFor(); InputStreamReader in = new InputStreamReader( p.getInputStream()); BufferedReader br = new BufferedReader(in); String lineStr; while ((lineStr = br.readLine()) != null) { System.out.println(lineStr); } } catch (Exception e) { e.printStackTrace(); } } }
http://www.linuxidc.com/Linux/2012-07/64526.htm
本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1700098
Runtime的使用 Runtime 又叫运行时,是一套底层的 C 语言 API,其为 iOS 内部的核心之一,我们平时编写的 OC 代码,底层都是基于它来实现的。
OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: ip : executable file not found in $PATH: unknown (Docker容器没有ip addr命令:exec ip addr 报错) OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: ip : executable file not found in $PATH: unknown (Docker容器没有ip addr命令:exec ip addr 报错)
相关文章
- 昇腾AI处理器软件栈--运行管理器(Runtime)
- VUE - vue.runtime.esm.js?6e6d:619 [Vue warn]: Do not use built-in or reserved HTML elements as component i
- Runtime(动态添加属性)
- Java魔法堂:找外援的利器——Runtime.exec详解
- [HTML 5 Performance] Benchmark functions runtime in chrome console
- Android开发者指南(21) —— Handling Runtime Changes
- api-ms-win-crt-runtime-l1-1-0.dll丢失问题
- System.Runtime.InteropServices.COMException (0x8004E00F): COM+ 无法与 Microsoft 分布式事务协调程序交谈 (异常来自 HRESU
- atitit 虚拟机之道vm之道 runtime设计 运行时 .attilax著.docx 1. Atitit 虚拟机的层次 架构与常见的虚拟机3 1.1. Os隔离了硬件的区别4 1.2.
- 【Android 逆向】ART 脱壳 ( 修改 /art/runtime/dex_file.cc#OpenCommon 系统源码进行脱壳 )
- 【Android 逆向】ART 脱壳 ( DexClassLoader 脱壳 | exec_utils.cc 中执行 Dex 编译为 Oat 文件的 Exec 和 ExecAndReturnC函数 )
- Microsoft Visual C++ Runtime Library Runtime Error解决的方式
- iOS开发RunTime之函数调用
- Java 利用 ArcGIS Runtime SDK for Java 轻松导出地图服务:高效、可定制的地图数据处理方案
- tensorflow报错:Failed to load the native TensorFlow runtime.