Re: [問題] 在Linux底下執行外部程式?

看板java作者 (CJV)時間15年前 (2010/10/30 16:06), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《CSLabor (電腦工人)》之銘言: : 我在Linux底下已經寫了一個簡單的helloworld執行檔 : 檔名叫helloworld : 用途是print出一串helloworld : 現在我要用java程式呼叫這個執行檔 : 這個Java程式檔名叫做helloworld.java : helloworld.java內容如下 : import java.io.*; : public class helloworld{ : public static void main(String[] args){ : try{ : String[] command={"/bin/sh","./helloworld"}; : Runtime.getRuntime().exec(command); : } : catch(IOException e){ : e.printStackTrace(); : } : } : } : 我的程式編譯執行後沒有反應 : 可以請有經驗的大大可以幫我看一下嗎? : 在這邊先謝謝有經驗的前輩 如果你的 helloworld 是 .sh 檔的話,那麼這樣寫應該是沒問題 如果是單一執行檔,是否還需要透過 sh 也是一個問題... 因為在 windows 底下有 .exe 也是無法執行的情況,需要透過 cmd cmd /C ???.exe 這樣 如果要取得那個程式輸出的資料: Process p=Runtime.getRuntime().exec(command); 然後 p.getInputStream() 取得 InputStream (個人習慣是餵給 Scanner 吃) 然後再印出來,如; Process p=Runtime.getRuntime().exec(command); Scanner ipt=new Scanner(p.getInputStream()); while(ipt.hasNextLine()) System.out.println(ipt.nextLine()); -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.123.84.166

10/30 18:27, , 1F
謝謝兩位前輩的幫忙
10/30 18:27, 1F
文章代碼(AID): #1Coz7tWx (java)
文章代碼(AID): #1Coz7tWx (java)