執行 exe 是沒問題,但若要在呼叫其他的java 程式我嘗試會沒反應,這點應該是索引位置判別錯誤的關係。
若將java 檔包成可執行檔如 jar 我想可以正常呼叫
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class JPing {
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
Process process = null;
String line = null;
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
String ip = "127.0.0.1"; //要Ping 的IP位址
try {
process = runtime.exec("ping " + ip);
is = process.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
while ( (line = br.readLine()) != null) {
System.out.println(line);
System.out.flush();
}
is.close();
isr.close();
br.close();
System.out.println("Java 呼叫 ping 程式,執行完畢!");
}
catch (IOException e) {
System.out.println(e);
runtime.exit(0);
}
}//end main method
}//end class
沒有留言:
張貼留言