Processexec(String command)
          在单独的进程中执行指定的字符串命令。
 Processexec(String[] cmdarray)
          在单独的进程中执行指定命令和变量。
 Processexec(String[] cmdarray, String[] envp)
          在指定环境的独立进程中执行指定命令和变量。
 Processexec(String[] cmdarray, String[] envp, File dir)
          在指定环境和工作目录的独立进程中执行指定的命令和变量。
 Processexec(String command, String[] envp)
          在指定环境的单独进程中执行指定的字符串命令。
 Processexec(String command, String[] envp, File dir)
          在有指定环境和工作目录的独立进程中执行指定的字符串命令。


dir 指定了新子进程的工作目录。如果 dirnull,那么子进程会继承当前进程的当前工作目录


waitFor导致当前线程等待,如有必要,一直要等到由该 Process 对象表示的进程已经终止。如果已终止该子进程,此方法立即返回。如果没有终止该子进程,调用的线程将被阻塞,直到退出子进程,0 表示正常终止。


wKiom1YQrH2AsvzFAAB0v4GqoLo557.jpg

一些平台只为标准输入输出提供有限的缓存。错误的写子进程的输入流或者错误的都子进程的输出流都有可能造成子进程的阻塞,甚至是死锁。


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