posts - 6,  comments - 3,  trackbacks - 0

          調用外部應用程序(譬如VB,有應用程序窗口的情況)

          import java.io.*;
          public class execOP {

           public execOP(){
            //TODO
           }
           
           /**
            * 執行外部的程序(參數為數組).返回程序的輸出(不具有輸入的功能)
            * @param appParam   程序及參數組成的數組(每個參數為一個數組成員)
            * @return
            * @throws Exception
            */
           public String execExternalApp(String []appParam) throws Exception{
            String str="";
            
            Process proc=Runtime.getRuntime().exec(appParam);
            
            DataInputStream in = new DataInputStream(proc.getInputStream());
            DataInputStream error = new DataInputStream(proc.getErrorStream());
            
            try
            {
             String tmp="";
             while ((tmp= in.readLine()) != null) {
              str+="控制臺輸出:"+tmp+"\n";      
              //System.out.println("控制臺?出:"+tmp);  
              }
             while ((tmp= error.readLine()) != null) {     
              str+="錯誤輸出:"+tmp+"\n";             
              //System.out.println("???出:"+tmp);
             }
            }
            catch(Exception e)
            {
              System.out.println("獲取應用程序輸出時發生IO錯誤"+e.getMessage());
            }
            return str;
           }
           
           /**
            * 執行外部的程序(參數為字符串).返回程序的輸出(不具有輸入的功能)
            * @param appParam  程序及參數組成的數組(每個參數為一個數組成員)
            * @return
            * @throws Exception
            */
           public String execExternalApp(String appParam) throws Exception{
            String str="";
            
            Process proc = Runtime.getRuntime().exec(appParam);
            DataInputStream in = new DataInputStream(proc.getInputStream());
            DataInputStream error = new DataInputStream(proc.getErrorStream());
            
            try{
             String tmp="";
             while ((tmp= in.readLine()) != null) {  
              str+="空隻臺輸出:"+tmp+"\n";            
              //System.out.println("控制臺?出:"+tmp);
             }
             while ((tmp= error.readLine()) != null) {         
              str+="錯誤輸出:"+tmp+"\n";               
              //System.out.println("???出:"+tmp);  
             }
            }
            catch(Exception e){
              System.out.println("獲取應用程序輸出時發生IO錯誤:"+e.getMessage());
            }
            return str;
           }
           
           /**
            * 測試程序
            * @param args
            */
           public static void main(String[] args) {
            execOP exec=new execOP();
            
            String appcmd="\\\\IP\\c$\\Program Files\\WIPTracking\\XraySystem.exe";
            //String appcmd=args[0];
            try{
              System.out.println(exec.execExternalApp(appcmd));
            }
            catch(Exception e){
             
            }

           }

          }

          posted on 2005-10-24 13:38 Java&Inter 閱讀(767) 評論(0)  編輯  收藏 所屬分類: Java技術

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(1)

          隨筆分類(3)

          隨筆檔案(6)

          文章分類(24)

          文章檔案(22)

          收藏夾(2)

          經常去的Blog

          • Eclipse/GEF
          • 關于Eclipse插件(plugins)開發的心得,主要包括:SWT/JFACE/GEF/EMF/RCP

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 东安县| 镇赉县| 沐川县| 阜新| 杭州市| 南江县| 常熟市| 大田县| 惠安县| 丰顺县| 扎囊县| 云南省| 陆河县| 江门市| 徐闻县| 济源市| 湖南省| 昭平县| 丹江口市| 邹平县| 金坛市| 墨玉县| 乾安县| 庆阳市| 台江县| 黄浦区| 景洪市| 金湖县| 甘泉县| 杭州市| 花垣县| 博乐市| 桃江县| 华坪县| 广平县| 无为县| 芦溪县| 新乡县| 永川市| 固原市| 闻喜县|