java獲取程序運行時RT打開qq

代碼:

import java.io.IOException;  


public class TestRT {  
  
    /** 
     * 使用Runtime對象的exec方法,調用外部exe文件。 
     */  
    public static void main(String[] args) {  
        Runtime rt = Runtime.getRuntime();  
        try {  
            rt.exec("mspaint.exe");    //打開畫圖程序
            rt.exec("C:\\Program Files (x86)\\Tencent\\QQ\\QQProtect\\Bin\\QQProtect.exe");   //"C:\\Program Files 

                                                                                                                  //(x86)\\Tencent\\QQ\\QQProtect\\Bin\\QQProtect.exe"
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
}  

說明:

     每個java運行程序都有一個Runtime運行實例,作用是使得應用程序與當前的運行環境對接。通過getRuntime()獲取實例。

     引用程序不能創建該實例(環境嘛,只能獲取)

   1. static Runtime getRuntime() 
          返回與當前 Java 應用程序相關的運行時對象。 
  2. Process exec(String command) 
          在單獨的進程中執行指定的字符串命令。
 



代碼2:

  1. import java.io.BufferedReader;  
  2. import java.io.IOException;  
  3. import java.io.InputStreamReader;  
  4. import javax.swing.JOptionPane;  
  5.   
  6. public class TestRT {  
  7.     public final static int END_MARK = 0;  
  8.   
  9.     /** 
  10.      * 使用Runtime對象的exec方法,運行cmd命令。 
  11.      */  
  12.     public static void main(String[] args) {  
  13.         Runtime rt = Runtime.getRuntime();  
  14.         try {         
  15.             Process pr = rt.exec("ping www.hao123.com "); //運行cmd命令  
  16.             BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));  
  17.             String s = br.readLine();  
  18.             String temp = "" ;  
  19.             while(null != s ){  
  20.                 if(!"".equals(s.trim()))  temp = s;  
  21.                 System.out.println(s);  
  22.                 s = br.readLine();  
  23.             }  
  24.             br.close();  
  25.             //導致當前線程等待,如果必要,一直要等到由該 Process 對象表示的進程已經終止。  
  26.             pr.waitFor();   
  27.             //此 Process 對象表示的子進程的出口值。根據慣例,值 0 表示正常終止。  
  28.             if (END_MARK == pr.exitValue()) {  
  29.                 JOptionPane.showMessageDialog(null, temp );  
  30.             }  
  31.         } catch (IOException e) {  
  32.             e.printStackTrace();  
  33.         } catch (InterruptedException e) {  
  34.             e.printStackTrace();  
  35.         }  
  36.     }  
  37. }  
   解釋: Process.waitFor(),該方法會導致當前process堵塞,直到process線程推出,然後繼續運行waitFor()後面的方法。
exp:
public class ProcessDemo {

   public static void main(String[] args) {
      try {
         // create a new process
         System.out.println("Creating Process...");
         Process p = Runtime.getRuntime().exec("notepad.exe");

         // cause this process to stop until process p is terminated

         p.waitFor();//導致main主線程堵塞,直到Process p中斷terminated,繼續主線程繼續運行下去

         // when you manually close notepad.exe program will continue here (導致mian主線程堵塞,直到)
         System.out.println("Waiting over.");

      } catch (Exception ex) {
         ex.printStackTrace();
      }

   }
}
result:
Creating Process... 
(堵塞中。。。直到關閉note 輸出waiting over,此時如果調用Process.exitValue(),可以得到0(程序正常結束) 1(非正常中斷))
Waiting over.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章