使用Runtime.getRuntime().exec()方法的几个陷阱 (转)

The class java.lang.Runtime features a static method called getRuntime(), which retrieves the current Java Runtime Environment.
That is the only way to obtain a reference to the Runtime object. With that reference, you can run external programs by invoking the Runtime class‘s exec() method. Developers often call this method to launch a browser for displaying a help page in HTML. 
There are four overloaded versions of the exec() command: 
public Process exec(String command); 
public Process exec(String [] cmdArray); 
public Process exec(String command, String [] envp); 
public Process exec(String [] cmdArray, String [] envp);

1.Stumbling into an IllegalThreadStateException 
The first pitfall relating to Runtime.exec() is the IllegalThreadStateException.

import java.util.*;
import java.io.*;
public class BadExecJavac
{
    public static void main(String args[])
    {
        try
        {
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec("javac");
            int exitVal = proc.exitValue();
            System.out.println("Process exitValue: " + exitVal);
        } catch (Throwable t){t.printStackTrace();}
    }
}

If an external process has not yet completed, the exitValue() method will throw an IllegalThreadStateException; that‘s why this program failed. While the documentation states this fact, why can‘t this method wait until it can give a valid answer? 
 
A more thorough look at the methods available in the Process class reveals a waitFor() method that does precisely that. In fact, waitFor() also returns the exit value, which means that you would not use exitValue() and waitFor() in conjunction with each other, but rather would choose one or the other. The only possible time you would use exitValue() instead of waitFor() would be when you don‘t want your program to block waiting on an external process that may never complete.

import java.util.*;
import java.io.*;

public class BadExecJavac2
{
    public static void main(String args[])
    {
        try
        {
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec("javac");
            int exitVal = proc.waitFor();
            System.out.println("Process exitValue: " + exitVal);
        } catch (Throwable t){t.printStackTrace();}
    }
}

Unfortunately, a run of BadExecJavac2 produces no output. The program hangs and never completes. 
Why does the javac process never complete? 
Why Runtime.exec() hangs 
The JDK‘s Javadoc documentation provides the answer to this question: 
Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock. 
本地平台为标准的输入输出流只能提供有限的buffer,因此就不能够立即写子进程的输入流或者是读取子进程的输出流,这就会导致子进程的阻塞甚至死锁

import java.util.*;
import java.io.*;
public class MediocreExecJavac
{
    public static void main(String args[])
    {
        try
        {
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec("javac");
            InputStream stderr = proc.getErrorStream();
            InputStreamReader isr = new InputStreamReader(stderr);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            System.out.println("<ERROR>");
            while ( (line = br.readLine()) != null)
                System.out.println(line);
            System.out.println("</ERROR>");
            int exitVal = proc.waitFor();
            System.out.println("Process exitValue: " + exitVal);
        } catch (Throwable t){t.printStackTrace();}
    }
}

So, MediocreExecJavac works and produces an exit value of 2. Normally, an exit value of 0 indicates success; any nonzero value indicates an error. The meaning of these exit values depends on the particular operating system.
Thus, to circumvent the second pitfall -- hanging forever in Runtime.exec() -- if the program you launch produces output or expects input, ensure that you process the input and output streams. 
3.Assuming a command is an executable program

import java.util.*;
import java.io.*;

public class BadExecWinDir
{
    public static void main(String args[])
    {
        try
        {
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec("dir");
            InputStream stdin = proc.getInputStream();
            InputStreamReader isr = new InputStreamReader(stdin);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            System.out.println("<OUTPUT>");
            while ( (line = br.readLine()) != null)
                System.out.println(line);
            System.out.println("</OUTPUT>");
            int exitVal = proc.waitFor();
            System.out.println("Process exitValue: " + exitVal);
        } catch (Throwable t)
          {
            t.printStackTrace();
          }
    }
}

java BadExecWinDir
java.io.IOException: CreateProcess: dir error=2
        at java.lang.Win32Process.create(Native Method)
        at java.lang.Win32Process.<init>(Unknown Source)
        at java.lang.Runtime.execInternal(Native Method)
        at java.lang.Runtime.exec(Unknown Source)
        at java.lang.Runtime.exec(Unknown Source)
        at java.lang.Runtime.exec(Unknown Source)
        at java.lang.Runtime.exec(Unknown Source)
        at BadExecWinDir.main(BadExecWinDir.java:12)
That‘s because the directory command is part of the Windows command interpreter and not a separate executable. To run the Windows command interpreter, execute either command.com or cmd.exe, depending on the Windows operating system you use.

import java.util.*;
import java.io.*;

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();
              }
    }
}

public class GoodWindowsExec
{
    public static void main(String args[])
    {
        if (args.length < 1)
        {
            System.out.println("USAGE: java GoodWindowsExec <cmd>");
            System.exit(1);
        }

        try
        {
            String osName = System.getProperty("os.name" );
            String[] cmd = new String[3];

            if( osName.equals( "Windows NT" ) )
            {
                cmd[0] = "cmd.exe" ;
                cmd[1] = "/C" ;
                cmd[2] = args[0];
            }
            else if( osName.equals( "Windows 95" ) )
            {
                cmd[0] = "command.com" ;
                cmd[1] = "/C" ;
                cmd[2] = args[0];
            }

            Runtime rt = Runtime.getRuntime();
            System.out.println("Execing " + cmd[0] + " " + cmd[1]
                               + " " + cmd[2]);
            Process proc = rt.exec(cmd);
            // any error message?
            StreamGobbler errorGobbler = new
                StreamGobbler(proc.getErrorStream(), "ERROR");            

            // any output?
            StreamGobbler outputGobbler = new
                StreamGobbler(proc.getInputStream(), "OUTPUT");

            // kick them off
            errorGobbler.start();
            outputGobbler.start();

            // any error???
            int exitVal = proc.waitFor();
            System.out.println("ExitValue: " + exitVal);
        } catch (Throwable t)
          {
            t.printStackTrace();
          }
    }
}

java GoodWindowsExec "dir *.java"
Running GoodWindowsExec with any associated document type will launch the application associated with that document type.
 For example, to launch Microsoft Word to display a Word document (i.e., one with a .doc extension), type: 
>java GoodWindowsExec "yourdoc.doc"
Thus, to avoid the third pitfall related to Runtime.exec(), do not assume that a command is an executable program; know whether you are executing a standalone executable or an interpreted command. 
It is important to note that the method used to obtain a process‘s output stream is called getInputStream(). The thing to remember is that the API sees things from the perspective of the Java program and not the external process. Therefore, the external program‘s output is the Java program‘s input. And that logic carries over to the external program‘s input stream, which is an output stream to the Java program. 
4.Runtime.exec() is not a command line One final pitfall to cover with Runtime.exec() is mistakenly assuming that exec() accepts any String that your command line (or shell) accepts. Runtime.exec() is much more limited and not cross-platform.

import java.util.*;
import java.io.*;

// StreamGobbler omitted for brevity

public class BadWinRedirect
{
    public static void main(String args[])
    {
        try
        {
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec("java jecho ‘Hello World‘ > test.txt");
            // any error message?
            StreamGobbler errorGobbler = new
                StreamGobbler(proc.getErrorStream(), "ERROR");            

            // any output?
            StreamGobbler outputGobbler = new
                StreamGobbler(proc.getInputStream(), "OUTPUT");

            // kick them off
            errorGobbler.start();
            outputGobbler.start();

            // any error???
            int exitVal = proc.waitFor();
            System.out.println("ExitValue: " + exitVal);
        } catch (Throwable t)
          {
            t.printStackTrace();
          }
    }
}
java BadWinRedirect
OUTPUT>‘Hello World‘ > test.txt
ExitValue: 0
The program BadWinRedirect attempted to redirect the output of an echo program‘s simple Java version into the file test.txt. However, we find that the file test.txt does not exist. The jecho program simply takes its command-line arguments and writes them to the standard output stream.
Instead, exec() executes a single executable (a program or script). If you want to process the stream to either redirect it or pipe it into another program, you must do so programmatically, using the java.io package.
import java.util.*;
import java.io.*;

class StreamGobbler extends Thread
{
    InputStream is;
    String type;
    OutputStream os;

    StreamGobbler(InputStream is, String type)
    {
        this(is, type, null);
    }

    StreamGobbler(InputStream is, String type, OutputStream redirect)
    {
        this.is = is;
        this.type = type;
        this.os = redirect;
    }

    public void run()
    {
        try
        {
            PrintWriter pw = null;
            if (os != null)
                pw = new PrintWriter(os);

            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line=null;
            while ( (line = br.readLine()) != null)
            {
                if (pw != null)
                    pw.println(line);
                System.out.println(type + ">" + line);
            }
            if (pw != null)
                pw.flush();
        } catch (IOException ioe)
            {
            ioe.printStackTrace();
            }
    }
}

public class GoodWinRedirect
{
    public static void main(String args[])
    {
        if (args.length < 1)
        {
            System.out.println("USAGE java GoodWinRedirect <outputfile>");
            System.exit(1);
        }

        try
        {
            FileOutputStream fos = new FileOutputStream(args[0]);
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec("java jecho ‘Hello World‘");
            // any error message?
            StreamGobbler errorGobbler = new
                StreamGobbler(proc.getErrorStream(), "ERROR");            

            // any output?
            StreamGobbler outputGobbler = new
                StreamGobbler(proc.getInputStream(), "OUTPUT", fos);

            // kick them off
            errorGobbler.start();
            outputGobbler.start();

            // any error???
            int exitVal = proc.waitFor();
            System.out.println("ExitValue: " + exitVal);
            fos.flush();
            fos.close();
        } catch (Throwable t)
          {
            t.printStackTrace();
          }
    }
}

java GoodWinRedirect test.txt
The solution to the pitfall was to simply control the redirection by handling the external process‘s standard output stream separately from the Runtime.exec() method. 
before finalizing arguments to Runtime.exec() and writing the code, quickly test the arguments. Listing 4.8 is a simple command-line utility that allows you to do just that.

import java.util.*;
import java.io.*;

// class StreamGobbler omitted for brevity

public class TestExec
{
    public static void main(String args[])
    {
        if (args.length < 1)
        {
            System.out.println("USAGE: java TestExec \"cmd\"");
            System.exit(1);
        }

        try
        {
            String cmd = args[0];
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec(cmd);

            // any error message?
            StreamGobbler errorGobbler = new
                StreamGobbler(proc.getErrorStream(), "ERR");            

            // any output?
            StreamGobbler outputGobbler = new
                StreamGobbler(proc.getInputStream(), "OUT");

            // kick them off
            errorGobbler.start();
            outputGobbler.start();

            // any error???
            int exitVal = proc.waitFor();
            System.out.println("ExitValue: " + exitVal);
        } catch (Throwable t)
          {
            t.printStackTrace();
          }
    }
}

java TestExec "explorer.exe aa.html"
To sum up, follow these rules of thumb to avoid the pitfalls in Runtime.exec(): 
1.      You cannot obtain an exit status from an external process until it has exited 
2.      You must immediately handle the input, output, and error streams from your spawned external process 
3.      You must use Runtime.exec() to execute programs 
4.      You cannot use Runtime.exec() like a command line

http://web4.blog.163.com/blog/static/1896941312009124102715446/

http://tech.it168.com/j/2006-04-29/200604291539038.shtml

时间: 2024-11-05 23:35:09

使用Runtime.getRuntime().exec()方法的几个陷阱 (转)的相关文章

Runtime.getRuntime().exec方法

Runtime.getRuntime().exec共有四个重载方法: public Process exec(String command) 在单独的进程中执行指定的字符串命令. public Process exec(String [] cmdArray) 在单独的进程中执行指定命令和变量 public Process exec(String command, String [] envp) 在指定环境的独立进程中执行指定命令和变量 public Process exec(String []

Android ProcessBuilder与Runtime.getRuntime().exec分别创建进程的区别

在Android中想要进行Ping,在不Root机器的情况下似乎还只能进行底层命调用才能实现. 因为在Java中要进行ICMP包发送需要Root权限. 于是只能通过创建进程来解决了,创建进程在Java中有两种方式,分别为: 1. 调用ProcessBuilder的构造函数后执行start() 2. 用Runtime.getRuntime().exec()方法执行 经过使用后发现两者有区别但是也并不是很大,两个例子说明: 1.调用ProcessBuilder的构造函数后执行start(): Pro

Runtime.getRuntime().exec中命令含有括号问题

在写批量执行bat工具的时候,想起了之前写的定时小工具里面的执行方法. 使用Runtime.getRuntime().exec方法. Runtime.getRuntime().exec("cmd /c start c:/test.bat") 这样就可以像dos窗口直接执行命令行一样. getRuntime返回Runtime的实例化对象,exec方法是返回Process对象. 查看Process类可以发现: The ProcessBuilder.start() and Runtime.e

Runtime.getRuntime.exec()执行linux脚本导致程序卡死有关问题

Runtime.getRuntime.exec()执行linux脚本导致程序卡死问题问题: 在Java程序中,通过Runtime.getRuntime().exec()执行一个Linux脚本导致程序被挂住,而在终端上直接执行这个脚本则没有任何问题.原因: 先来看Java代码: public final static void process1(String[] cmdarray) {        Process p = null;        BufferedReader br = null

Runtime.getRuntime().exec()调用外部程序

场景:linux下,在web工程里调用一个C++程序,实现代码如下: StringBuffer cmd = new StringBuffer();cmd.append("nohup "); ……System.out.println("执行程序命令:"+cmd.toString());String[] cmds = { "/bin/sh", "-c", cmd.toString()};Runtime.getRuntime().e

Runtime.getRuntime().exec()----记录日志案例

Runtime.getRuntime().exec()方法主要用于执行外部的程序或命令. Runtime.getRuntime().exec共有六个重载方法: 1.public Process exec(String command) 在单独的进程中执行指定的字符串命令. 2.public Process exec(String [] cmdArray) 在单独的进程中执行指定命令和变量 3.public Process exec(String command, String [] envp)

用Runtime.getRuntime().exec()需要注意的地方

d有时候我们可能需要调用系统外部的某个程序,此时就可以用Runtime.getRuntime().exec()来调用,他会生成一个新的进程去运行调用的程序. 此方法返回一个java.lang.Process对象,该对象可以得到之前开启的进程的运行结果,还可以操作进程的输入输出流. Process对象有以下几个方法: 1.destroy() 杀死这个子进程 2.exitValue() 得到进程运行结束后的返回状态 3.waitFor() 得到进程运行结束后的返回状态,如果进程未运行完毕则等待知道执

Runtime.getRuntime().exec()需要注意的地方

文章出处http://www.cnblogs.com/fclbky/p/6112180.html 有时候我们可能需要调用系统外部的某个程序,此时就可以用Runtime.getRuntime().exec()来调用,他会生成一个新的进程去运行调用的程序. 此方法返回一个java.lang.Process对象,该对象可以得到之前开启的进程的运行结果,还可以操作进程的输入输出流. Process对象有以下几个方法: 1.destroy() 杀死这个子进程 2.exitValue() 得到进程运行结束后

Java Runtime.getRuntime().exec 执行带空格命令解决办法

String command = OpenOffice_HOME + "program\\soffice -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard "; command = "cmd /c start "+command.replaceAll(" ","\" \""); P