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 阅读(744) 评论(0)  编辑  收藏 所属分类: Java技术

只有注册用户登录后才能发表评论。


网站导航:
 

<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(1)

随笔分类(3)

随笔档案(6)

文章分类(24)

文章档案(22)

收藏夹(2)

经常去的Blog

  • Eclipse/GEF
  • 关于Eclipse插件(plugins)开发的心得,主要包括:SWT/JFACE/GEF/EMF/RCP

搜索

  •  

最新评论

阅读排行榜

评论排行榜