runtime.exec()
来源:互联网 发布:windows无法格式化 编辑:程序博客网 时间:2024/06/13 12:22
Process
exec(String command)
在单独的进程中执行指定的字符串命令。
Process
exec(String[] cmdarray)
在单独的进程中执行指定命令和变量。
Process
exec(String[] cmdarray, String[] envp)
在指定环境的独立进程中执行指定命令和变量。
Process
exec(String[] cmdarray, String[] envp, File dir)
在指定环境和工作目录的独立进程中执行指定的命令和变量。
Process
exec(String command, String[] envp)
在指定环境的单独进程中执行指定的字符串命令。
Process
exec(String command, String[] envp, File dir)
在有指定环境和工作目录的独立进程中执行指定的字符串命令。
dir 指定了新子进程的工作目录。如果 dir 为 null,那么子进程会继承当前进程的当前工作目录
waitFor导致当前线程等待,如有必要,一直要等到由该 Process
对象表示的进程已经终止。如果已终止该子进程,此方法立即返回。如果没有终止该子进程,调用的线程将被阻塞,直到退出子进程,0
表示正常终止。
一些平台只为标准输入输出提供有限的缓存。错误的写子进程的输入流或者错误的都子进程的输出流都有可能造成子进程的阻塞,甚至是死锁。
getErrorStream得到错误信息
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;public class Runshell1 { public static void main(String[] args) { try { /* 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(); } }}
linux下代码
package com.zxing.imgQRCode;import java.io.BufferedReader;import java.io.InputStreamReader;public class Runshell1 { public static void main(String[] args) { try { 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(); } }}
Java执行带重定向或管道的shell命令的问题
http://www.linuxidc.com/Linux/2012-07/64526.htm
本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1700098
- runtime.exec
- Runtime.exec()
- runtime exec
- runtime.exec()
- Runtime.getRuntime(). exec( )
- Runtime的exec方法
- Runtime.exec()的使用
- java Runtime.getRuntime().exec
- Runtime.getRuntime().exec
- Runtime.getRuntime().exec()
- Java Runtime.exec()注意事项
- Runtime.exec()的使用
- Java Runtime.exec()注意事项
- java runtime.exec()
- Runtime.getRuntime().exec()
- java Runtime.getRuntime().exec()
- Runtime.getRuntime.exec 注意事项
- Runtime.exec() 的陷阱
- Mapreduce构建hbase二级索引
- 一致性Hash算法(分布式哈希)
- 多线程同步基础
- flume+kafka+hdfs详解
- java读取Excel文件
- runtime.exec()
- sqoop详解
- spark概述与编程模型
- storm-kafka(storm spout作为kafka的消费端)
- storm详解
- java中long和Long的区别
- spark内核
- spark调优
- Avro数据序列化