JavaWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JavaWorld@TW » Java 新手區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 [分享]使用Java呼叫Windows的外部程式(如:Ping)
sungo

瘋狂口罩大盜



發文: 822
積分: 17
於 2004-02-06 09:02 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
之前看過一則JSP程式是用來呼叫外部程
式的,我覺得是蠻不錯的範例,所以把它
改成Application版,跟大家分享一下!


註:Windows的ping指令是用來測試連線的。如:
1
C:\ping 127.0.0.1


程式碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.io.*;
 
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


JPing.java (0.86k)


vote up 0 vote down
sungo edited on 2004-02-06 09:05
reply to postreply to post

話題樹型展開
人氣 標題 作者 字數 發文時間
9033 [分享]使用Java呼叫Windows的外部程式(如:Ping) sungo 1077 2004-02-06 09:02
2684 Re:[分享]使用Java呼叫Windows的外部程式(如:Ping) 神熾 12 2007-06-26 12:53
1631 Re:[分享]使用Java呼叫Windows的外部程式(如:Ping) 49140029 28 2008-04-22 11:52
874 Re:[分享]使用Java呼叫Windows的外部程式(如:Ping) chih0812 189 2008-12-12 15:47
815 Re:[分享]使用Java呼叫Windows的外部程式(如:Ping) chih0812 40 2008-12-12 15:52
826 Re:[分享]使用Java呼叫Windows的外部程式(如:Ping) Duncan 419 2008-12-12 15:56
813 Re:[分享]使用Java呼叫Windows的外部程式(如:Ping) chih0812 51 2008-12-12 16:07
876 Re:[分享]使用Java呼叫Windows的外部程式(如:Ping) chih0812 822 2008-12-12 16:10
» JavaWorld@TW »  Java 新手區

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JavaWorld@TW


Powered by Powerful JuteForum® Version Jute 1.5.8
Copyright© 2002-2003 Rainman Zhu,Zua,Netboy,Scott. All Rights Reserved.