java.lang.Runtime.exec的左膀右臂:流輸入和流讀取詳解
在java.lang.Runtime.exec的使用中,我們經(jīng)常會用到將重定向命令執(zhí)行的輸入/結(jié)果或者將錯誤信息讀取出來.
那么,在使用過程中,我們?nèi)绾握_的使用呢?
什么是java.lang.Runtime
首先我們要明確一點,什么是Java.lang.Runtime? 我們來看官方[->link<-]的描述:
" Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime method.
An application cannot create its own instance of this class. "
也就是說,Runtime是每個java-application運行時有且僅有一個的當(dāng)前實例.允許Application接入當(dāng)前運行環(huán)境.
我們再來看看Runtime的exec()方法:
" Executes the specified command in a separate process. "
這個方法可以讓我們在一個進(jìn)程中執(zhí)行指定的命令.其返回類型是Process類.
那么我們還需要來看一下Process類:
什么是java.lang.Process
什么是Java.lang.Process? 我們來看官方[->link<-]的描述:
"The class Process provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process."
也就是說這個類提供控制線程的方法.
我們再來看看Process提供的獲取輸入流和輸出流的方法:
public abstract InputStream getInputStream()
"Returns the input stream connected to the normal output of the subprocess.
The stream obtains data piped from the standard output of the process represented by this Process object."
public abstract OutputStream getOutputStream()
"Returns the output stream connected to the normal input of the subprocess.
Output to the stream is piped into the standard input of the process represented by this Process object."public abstract InputStream getErrorStream()
"Returns the input stream connected to the error output of the subprocess.
The stream obtains data piped from the error output of the process represented by this Process object."
到這里,我們就明白里其中的因果>從exec()返回的Process的對象中調(diào)用獲取流的方法.從而達(dá)到目的!
具體做法
在需要使用exec()去執(zhí)行命令并獲取返回值的時候,具體的做法是什么呢?
僅僅圍繞這個思路:"從exec()返回的Process的對象中調(diào)用獲取流的方法getXStream"
String s = null;
Process p = Runtime
.getRuntime()
.exec(
new String[]{"/bin/sh",
"-c",
"java HelloWorld"},null,dir);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
//打印出輸出結(jié)果
log.info("標(biāo)準(zhǔn)輸出命令");
while ((s = stdInput.readLine()) != null) {
log.info(s);
}
log.info("標(biāo)準(zhǔn)錯誤的輸出命令");
while ((s = stdError.readLine()) != null) {
log.info(s);
}
其中
dir 是我的HelloWorld.class的存放目錄,只能這樣用,請參照這篇
HelloWorld.java 的內(nèi)容是Sysotem.out.println打印幾行Hello World,此處沒有編譯,使用時應(yīng)注意.

到此,大功告成!
Runtime.exec 陷阱
該類java.lang.Runtime具有一個稱為的靜態(tài)方法getRuntime(),該方法檢索當(dāng)前的Java Runtime Environment。這是獲得對該Runtime對象的引用的唯一方法。使用該參考,您可以通過調(diào)用Runtime類的exec()方法來運行外部程序。開發(fā)人員經(jīng)常調(diào)用此方法來啟動瀏覽器,以顯示HTML的幫助頁面。
該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);
對于這些方法中的每一個,命令(可能還有一組參數(shù))都傳遞給特定于操作系統(tǒng)的函數(shù)調(diào)用。隨后,這將參考Process返回給Java VM的類來創(chuàng)建特定于操作系統(tǒng)的進(jìn)程(正在運行的程序)。所述Process類是一個抽象類,因為一個特定的子類Process存在于每個操作系統(tǒng)。
您可以將三個可能的輸入?yún)?shù)傳遞給這些方法:
- 一個字符串,代表要執(zhí)行的程序和該程序的所有參數(shù)
- 字符串?dāng)?shù)組,用于將程序與其參數(shù)分開
- 一組環(huán)境變量
以形式傳遞環(huán)境變量name=value。如果exec()對程序及其參數(shù)使用單個字符串的版本,請注意,通過StringTokenizer類使用空格作為分隔符來解析該字符串。
IllegalThreadStateException
要執(zhí)行Java VM外部的進(jìn)程,我們使用exec()方法。要查看外部進(jìn)程返回的值,我們exitValue()。
如果外部過程尚未完成,則該exitValue()方法將拋出IllegalThreadStateException;。這就是該程序失敗的原因。盡管文檔中說明了這一事實,但為什么不能等到此方法可以給出有效答案呢?
對該Process類中可用的方法進(jìn)行更徹底的研究,就會發(fā)現(xiàn)waitFor()可以做到這一點的方法。實際上,waitFor()還會返回退出值,這意味著您將不會使用exitValue()和waitFor()彼此結(jié)合,而是會選擇一個或另一個。你會使用的唯一可能的時間exitValue(),而不是waitFor()會當(dāng)你不希望你的程序阻止等待外部過程中可能永遠(yuǎn)不會完成。與其使用該waitFor()方法,不如將一個被調(diào)用的布爾參數(shù)waitFor傳入該exitValue()方法以確定當(dāng)前線程是否應(yīng)等待。布爾值會更有利,因為exitValue()是此方法的更合適的名稱,兩個方法在不同條件下不必執(zhí)行相同的功能。這種簡單的條件判別是輸入?yún)?shù)的領(lǐng)域。
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();
}
}
}
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();
}
}
}
因此,為避免此陷阱,請捕獲IllegalThreadStateException或等待該過程完成。
為什么Runtime.exec()掛起
由于某些本機平臺僅為標(biāo)準(zhǔn)輸入和輸出流提供有限的緩沖區(qū)大小,因此未能及時寫入子流程的輸入流或讀取子流程的輸出流可能導(dǎo)致子流程阻塞,甚至死鎖。
現(xiàn)在,讓我們關(guān)注JDK文檔并處理javac過程的輸出。當(dāng)您javac不帶任何參數(shù)運行時,它會生成一組用法語句,這些用法語句描述了如何運行程序以及所有可用程序選項的含義。知道這將stderr流到流,您可以輕松地編寫一個程序以在等待進(jìn)程退出之前耗盡該流。清單4.3完成了該任務(wù)。盡管此方法行之有效,但這不是一個好的通用解決方案。因此,清單4.3的程序被命名為MediocreExecJavac;。它僅提供平庸的解決方案。更好的解決方案將同時清空標(biāo)準(zhǔn)錯誤流和標(biāo)準(zhǔn)輸出流。最好的解決方案是同時清空這些流(稍后再說明)
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();
}
}
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring @Scheduler使用cron表達(dá)式時的執(zhí)行問題詳解
Spring給程序猿們帶來了許多便利。下面這篇文章主要給大家介紹了關(guān)于Spring @Scheduler使用cron表達(dá)式時的執(zhí)行問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09
圖文講解Java中實現(xiàn)quickSort快速排序算法的方法
這篇文章主要介紹了Java中實現(xiàn)quickSort快速排序算法的方法,文章最后還介紹了一種單向掃描的實現(xiàn)方法,需要的朋友可以參考下2016-05-05
如何解決Maven打包時每次都出現(xiàn)Download maven-metadata.xml卡住問題
這篇文章主要介紹了如何解決Maven打包時每次都出現(xiàn)Download maven-metadata.xml卡住問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
基于bufferedreader的read()與readline()讀取出錯原因及解決
這篇文章主要介紹了bufferedreader的read()與readline()讀取出錯原因及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
Spring Data JPA 復(fù)雜/多條件組合分頁查詢
本文主要介紹了Spring Data JPA 復(fù)雜/多條件組合分頁查詢的相關(guān)資料。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04
Java并發(fā)編程之ReadWriteLock讀寫鎖的操作方法
這篇文章主要介紹了Java并發(fā)編程之ReadWriteLock讀寫鎖的操作方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02
基于Java的度分秒坐標(biāo)轉(zhuǎn)純經(jīng)緯度坐標(biāo)的漂亮國基地信息管理的方法
本文以java語言為例,詳細(xì)介紹如何管理漂亮國的基地信息,為下一步全球的空間可視化打下堅實的基礎(chǔ),首先介紹如何對數(shù)據(jù)進(jìn)行去重處理,然后介紹在java當(dāng)中如何進(jìn)行度分秒位置的轉(zhuǎn)換,最后結(jié)合實現(xiàn)原型進(jìn)行詳細(xì)的說明,感興趣的朋友跟隨小編一起看看吧2024-06-06

