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. "
這個方法可以讓我們在一個進程中執(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)的進程(正在運行的程序)。所述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外部的進程,我們使用exec()方法。要查看外部進程返回的值,我們exitValue()。
如果外部過程尚未完成,則該exitValue()方法將拋出IllegalThreadStateException;。這就是該程序失敗的原因。盡管文檔中說明了這一事實,但為什么不能等到此方法可以給出有效答案呢?
對該Process類中可用的方法進行更徹底的研究,就會發(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流到流,您可以輕松地編寫一個程序以在等待進程退出之前耗盡該流。清單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)文章
解決BigDecimal轉(zhuǎn)long丟失精度的問題
這篇文章主要介紹了解決BigDecimal轉(zhuǎn)long丟失精度的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12利用Java工具類Hutool實現(xiàn)驗證碼校驗功能
這篇文章主要介紹了利用Java工具類Hutool實現(xiàn)驗證碼校驗功能,利用Hutool實現(xiàn)驗證碼校驗,校驗的Servlet與今天的第一篇是一樣的,唯一就是驗證碼的生成是不一樣的,利用Hutool生成驗證碼更快捷.需要的朋友可以參考下2022-10-10IDEA 程序包不存在,找不到符號但是明明存在對應(yīng)的jar包(問題分析及解決方案)
這篇文章主要介紹了IDEA 程序包不存在,找不到符號但是明明存在對應(yīng)的jar包 的解決方案,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08使用自定義注解進行restful請求參數(shù)的校驗方式
這篇文章主要介紹了使用自定義注解進行restful請求參數(shù)的校驗方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10Maven+Tomcat8 實現(xiàn)自動化部署的方法
本篇文章主要介紹了Maven+Tomcat8 實現(xiàn)自動化部署的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10Java設(shè)計模式之單例模式Singleton Pattern詳解
這篇文章主要介紹了Java設(shè)計模式之單例模式Singleton Pattern詳解,一些常用的工具類、線程池、緩存,數(shù)據(jù)庫,數(shù)據(jù)庫連接池、賬戶登錄系統(tǒng)、配置文件等程序中可能只允許我們創(chuàng)建一個對象,這就需要單例模式,需要的朋友可以參考下2023-12-12關(guān)于兩個BeanUtils.copyProperties()的用法及區(qū)別
這篇文章主要介紹了關(guān)于兩個BeanUtils.copyProperties()的用法及區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06