原文地址:http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=4
作爲Java語言的一部分。java.lang包被隱藏的導入到每一個Java程序。這個包的表面陷阱,經常影響到大多數程序員。這個月,我將討論運行時exec()方法時的潛伏陷阱。
陷阱4:當運行exec()不會
exec()有四個重載:
public Process exec(String command);
public Process exec(String [] cmdArray);
public Process exec(String command, String [] envp);
public Process exec(String [] cmdArray, String [] envp);
對於每個這樣的方法,都會產生一個命令,並可能攜帶一組參數——被傳遞給一個特定操作系統的函數調用。這隨後創建一個特定操作系統的進程(一個運行着的程序),procss類將持有該程序返回Java VM的引用。這個procss類是一個抽象類,具體子類的實現依賴於不同的底層操作系統。
你可以通過三種可能的輸入參數到這些方法:
1、一個字符串,表示程序執行和程序的任何參數。
2、一個字符串數組,通過參數來區分出程序的實現功能。
3、一個環境變量的數組
傳遞環境變量是,使用格式化的方式:名稱=值。如果你使用單個字符串和它的參數的方式調用exec()的重載,,注意字符串是通過StringTokenizer類被解析,使用空格作爲分隔符。
陷入 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();
}
}
}
運行的BadExecJavac產生:
E:\classes\com\javaworld\jpitfalls\article2>java BadExecJavac
java.lang.IllegalThreadStateException: process has not exited
at java.lang.Win32Process.exitValue(Native Method)
at BadExecJavac.main(BadExecJavac.java:13)
如果一個外部進程尚未完成,exitValue()方法將拋出IllegalThreadStateException。這就是程序失敗的原因。儘管文檔聲明瞭這個事實,爲什麼這個方法不能等待一個有效性的結果返回?
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();
}
}
}
不幸的是,一個運行的BadExecJavac2不產生任何輸出。程序掛起、一直未完成。爲什麼javac進程一直沒有完成?
爲什麼 Runtime.exec() 掛起
因爲一些本機平臺只提供有限的緩衝區大小爲標準輸入和輸出流,未能及時寫輸入流或讀取輸出流的子流程可能會導致子流程阻止,甚至死鎖。
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();
}
}
}
運行
MediocreExecJava
產生:
E:\classes\com\javaworld\jpitfalls\article2>java MediocreExecJavac
<ERROR>
Usage: javac <options> <source files>
where <options> includes:
-g Generate all debugging info
-g:none Generate no debugging info
-g:{lines,vars,source} Generate only some debugging info
-O Optimize; may hinder debugging or enlarge class files
-nowarn Generate no warnings
-verbose Output messages about what the compiler is doing
-deprecation Output source locations where deprecated APIs are used
-classpath <path> Specify where to find user class files
-sourcepath <path> Specify where to find input source files
-bootclasspath <path> Override location of bootstrap class files
-extdirs <dirs> Override location of installed extensions
-d <directory> Specify where to place generated class files
-encoding <encoding> Specify character encoding used by source files
-target <release> Generate class files for specific VM version
</ERROR>
Process exitValue: 2
所以,MediocreExecJavac運行產生一個退出值2。通常,一個退出值0表示成功,任何非零值表示一個錯誤。這些退出值的含義取決於特定的操作系統。一個Win32錯誤值爲2是一個“未找到文件”錯誤。這是有道理的,因爲javac期望我們遵循的程序源代碼文件進行編譯。
假設一個命令是一個可執行程序
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();
}
}
}
運行
BadExecWinDir輸出:
E:\classes\com\javaworld\jpitfalls\article2>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)
如前所述,誤差值爲2的意思是“未找到文件”,在這種情況下,意味着可執行文件名爲dir.exe不能被發現。這是因爲目錄命令是Windows命令解釋器的一部分,而不是一個單獨的可執行文件。要運行Windows命令解釋器,執行orcmd.exe或者command.com,這取決於您使用的Windows操作系統。清單4.5運行的一個Windows命令解釋器,然後執行用戶提供的命令(如。,dir)。
清單 4.5 GoodWindowsExec.java
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();
}
}
}
使用dir命令,運行GoodWindowsExec產生:
E:\classes\com\javaworld\jpitfalls\article2>java GoodWindowsExec "dir *.java"
Execing cmd.exe /C dir *.java
OUTPUT> Volume in drive E has no label.
OUTPUT> Volume Serial Number is 5C5F-0CC9
OUTPUT>
OUTPUT> Directory of E:\classes\com\javaworld\jpitfalls\article2
OUTPUT>
OUTPUT>10/23/00 09:01p 805 BadExecBrowser.java
OUTPUT>10/22/00 09:35a 770 BadExecBrowser1.java
OUTPUT>10/24/00 08:45p 488 BadExecJavac.java
OUTPUT>10/24/00 08:46p 519 BadExecJavac2.java
OUTPUT>10/24/00 09:13p 930 BadExecWinDir.java
OUTPUT>10/22/00 09:21a 2,282 BadURLPost.java
OUTPUT>10/22/00 09:20a 2,273 BadURLPost1.java
... (some output omitted for brevity)
OUTPUT>10/12/00 09:29p 151 SuperFrame.java
OUTPUT>10/24/00 09:23p 1,814 TestExec.java
OUTPUT>10/09/00 05:47p 23,543 TestStringReplace.java
OUTPUT>10/12/00 08:55p 228 TopLevel.java
OUTPUT> 22 File(s) 46,661 bytes
OUTPUT> 19,678,420,992 bytes free
ExitValue: 0
GoodWindowsExec運行與任何相關的文檔類型將啓動與之關聯文檔類型的應用程序。例如,啓動Microsoft Word來顯示一個Word文檔(即一個帶doc擴展名的文件)。
>java GoodWindowsExec "yourdoc.doc"
注意,GoodWindowsExec使用操作系統的系統名字屬性來確定,您運行的是哪種Windows操作系統,從而確定適當的命令解釋器。
在執行命令解釋器,使用StreamGobbler類來處理標準錯誤和標準輸入流。StreamGobbler通過獨立線程,來清空任何傳遞到它的流。這個類使用一個簡單的字符串類型來表示流清空,在當它將打印行輸出到控制檯時起作用。
因此,爲了避免運行exec()的第三個陷阱,首先不要認爲一個命令是一個可執行程序;其次要了解你是否正在執行一個獨立的可執行文件或一種解釋命令。在結束這一節中,我將演示一個簡單的命令行工具,可以幫你分析。
值得注意的是,用於獲取一個進程的輸出流的方法叫做getInputStream()。要記住的是,API看待對象的角度,是從Java程序內部,而不是外部的過程。因此,外部程序的輸出是Java程序的輸入。這種邏輯,也表明外部程序的輸入流,是一個Java程序的輸出流。
Runtime.exec()不是命令行
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();
}
}
}
運行BadWinRedirect 產生:
E:\classes\com\javaworld\jpitfalls\article2>java BadWinRedirect
OUTPUT>'Hello World' > test.txt
ExitValue: 0
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();
}
}
}
運行 GoodWinRedirect 產生:
E:\classes\com\javaworld\jpitfalls\article2>java GoodWinRedirect test.txt
OUTPUT>'Hello World'
ExitValue: 0
在運行GoodWinRedirect、test.txt確實存在。解決這個陷阱很簡單,就是通過外部進程控制重定向的標準輸出流,獨立於exec()方法。我們創建一個單獨的OutputStream、讀取的文件名,打開文件,我們將從派生進程的標準輸出寫到文件。清單4.7通過對StreamGobbler類添加一個新的構造器來完成這個任務。新構造函數有三個參數:讀入輸入流,String類型標記我們是否正在使用讀入輸出流,重定向輸入。這個新版本的StreamGobbler並不違反任何代碼之前使用的,因爲我們沒有改變現有的公共API——我們只擴展它。
因爲參數來運行exec()是依賴於操作系統、不同系統之間的適用命令會有所變化。所以,在最終確定Runtime.exec()參數和編寫代碼,快速測試。清單4.8是一個簡單的命令行工具,允許你這樣做。
這是一個有用的練習:嘗試修改TestExec重定向標準輸入和標準輸出到一個文件。當在Windows 95或Windows 98上執行javac編譯器,這將解決錯誤消息超出命令行有限緩存的問題。
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();
}
}
}
E:\classes\com\javaworld\jpitfalls\article2>java TestExec "e:\java\docs\index.html"
java.io.IOException: CreateProcess: e:\java\docs\index.html error=193
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 TestExec.main(TestExec.java:45)
我們的第一個測試失敗,錯誤193。Win32誤差值193“不是一個有效的Win32應用程序。“這個錯誤告訴我們,沒有找到關聯的應用程序(如網景瀏覽器)存在,而且這一流程不能運行一個HTML文件沒有關聯的應用程序。
因此,我們嘗試測試,這次又給它一個完整路徑網景。(或者,我們可以添加到我們的PATH environment網景變量)。第二次運行的TestExec產生:
E:\classes\com\javaworld\jpitfalls\article2>java TestExec
"e:\program files\netscape\program\netscape.exe e:\java\docs\index.html"
ExitValue: 0
ok!網景瀏覽器的打開,然後裝入Java幫助文檔。
一個額外的改進包括一個命令行開關,使TestExec接受從標準輸入的輸入流。然後您將使用Process.getOutputStream()方法通過輸入派生外部程序。
你必須從你外部程序立即處理輸入、輸出和錯誤流
您必須使用運行時exec()來執行程序
你不能使用運行時執行()就像一個命令行