日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA shell grep

發布時間:2025/4/14 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA shell grep 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

近我的項目要我在WebService里用Java調用Linux下的Shell 腳本,在網上找了一些資料,以供學習。

地址:http://brian.pontarelli.com/2005/11/11/java-runtime-exec-can-hang/?????

Java Runtime exec can hang

November 11, 2005 on 4:40 pm | In?Java?|

The next version of Savant is going to focus heavily on the stand-alone runtime and support for dialects and plugins. Supporting all that is largely handled by using a simple executor framework I wrote around Java 1.4 and lower’s Runtime.exec method. A few things to keep in mind when using this:

  • Always read from the streams prior to calling waitFor. Otherwise you could end up waiting forever on Windows and other OS platforms whose I/O buffers can’t store enough from standard out and standard error to ensure the program has finished. These platforms will pause the execution of whatever is running until something reads the buffered content from standard out and standard error. I would imagine all platforms suffer from this, but some platforms have larger buffers than others. Needless to say, always read from the streams first.
  • Always read from standard error first. I ran across a bug where some OS platforms will always open standard out, but never close it. What this means is that if you read from standard out first and the process only writes to standard error, you’ll hang forever waiting to read. If you read from standard error first, you’ll always be okay on these platforms because the OS seems to shutdown standard error. I think however, that the best way to handle all cases is to check both standard error and standard out for readiness and only read from them if they have something to offer. The downside I could see here is that error isn’t ready, but eventually will be.
  • 可以看出:
    ?

    • 永遠要在調用waitFor()方法之前讀取數據流
    • 永遠要先從標準錯誤流中讀取,然后再讀取標準輸出流

    于是將waitFor()方法放在讀取數據流后調用,目前沒有發現什么問題。

    后面的build中在waitFor()之前讀取了數據流,bat文件就可以完整執行了:

    ?

    [java]?view plaincopy
  • Process?proc?=?Runtime.getRuntime().exec(cmd);??
  • ?????StreamGobbler?errorGobbler?=?new?StreamGobbler(proc.getErrorStream(),?"Error");??????????????
  • ?????????????????StreamGobbler?outputGobbler?=?new?StreamGobbler(proc.getInputStream(),?"Output");??
  • ?????????????????errorGobbler.start();??
  • ?????????????????outputGobbler.start();??
  • ??
  • ?????proc.waitFor();??
  • ??
  • 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)?{??
  • ????if?(type.equals("Error"))??
  • ?????LogManager.logError(line);??
  • ????else??
  • ?????LogManager.logDebug(line);??
  • ???}??
  • ??}?catch?(IOException?ioe)?{??
  • ???ioe.printStackTrace();??
  • ??}??
  • ?}??
  • }??
  • ?

    2. 另外一個需要注意的地方是:

    ???? 如果調用的腳本中存在像sudo這樣的需要tty的命令時,使用

    ?????????? String []cmdArray = new String[]{ "/bin/sh", "-c", "yourscriptname"};

    ?這樣的調用方式,會為腳本執行創建出一個tty環境,否則,運行過程會提示"sorry, you must have a tty to run xxx"的錯誤。

    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.

    在exec()后 立即調用waitFor()會導致進程掛起。

    ?

    3.當調用的外部命令中包含重定向(<、>),管道( | ) 命令時,exec(String command)的版本不能正確解析重定向、管道操作符。所以需要使用exec(String [] cmdArray)。

    ?? 如, echo "hello world" > /home/admin/newFile.txt

    ?????? ls -e | grep java

    ?? 需要使用如下的調用方式

    ?????? String []cmdArray = new String[]{ "/bin/sh", "-c", "ls -e | grep java"};

    ???????????? Runtime.getRuntime().exec(cmdArray);

    ?

    總結

    以上是生活随笔為你收集整理的JAVA shell grep的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。