簡體   English   中英

打開一個獨立的終端窗口並使用Java運行命令

[英]Open an independent terminal window and run command with Java

基本上,我試圖打開終端窗口並命令它啟動php腳本。
當腳本向終端輸出新行時,還能夠立即輸入並獲得輸出。 如果可能的話,我打算將其hidden

更像是將終端的輸出和輸入鏡像到Java應用程序本身

我做了

Runtime.getRuntime().exec("/usr/bin/open -a Terminal ~/Desktop/test.php"); //mac

一旦PHP腳本將輸出發送到終端,我不知道如何立即輸入和獲取輸出。

請在這里幫我

創建流程並讀取流程的輸入流。

(javadoc)getInputStream()

獲取子流程的輸入流。 該流從此Process對象表示的過程的標准輸出流中獲取通過管道傳輸的數據。

//-->check command line<--
Process process = Runtime.getRuntime().exec("/usr/bin/php /home/amit/hello.php");
BufferedInputStream iStream = new BufferedInputStream(process.getInputStream());
BufferedOutputStream oStream = new BufferedOutputStream(process.getOutputStream());

byte[] buffer = new byte[1024];
while (true){
   int length = iStream.read(buffer);
   if(length == -1)
      break;
   System.out.println(new String(buffer, 0, length));
}

注意:已針對Linux編寫。

與其在終端中執行php腳本(而不是在終端中使用php執行腳本),而是在php中執行php腳本並捕獲輸入/輸出。

$ which php
/usr/bin/php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM