Re: [問題] 在Linux底下執行外部程式?
※ 引述《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
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):