調(diào)用外部應(yīng)用程序(譬如VB,有應(yīng)用程序窗口的情況)
import java.io.*;
public class execOP {
public execOP(){
//TODO
}
/**
* 執(zhí)行外部的程序(參數(shù)為數(shù)組).返回程序的輸出(不具有輸入的功能)
* @param appParam 程序及參數(shù)組成的數(shù)組(每個參數(shù)為一個數(shù)組成員)
* @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("獲取應(yīng)用程序輸出時發(fā)生IO錯誤"+e.getMessage());
}
return str;
}
/**
* 執(zhí)行外部的程序(參數(shù)為字符串).返回程序的輸出(不具有輸入的功能)
* @param appParam 程序及參數(shù)組成的數(shù)組(每個參數(shù)為一個數(shù)組成員)
* @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("獲取應(yīng)用程序輸出時發(fā)生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 閱讀(764)
評論(0) 編輯 收藏 所屬分類:
Java技術(shù)