java调用应用程序_Java调用外部应用程序

这篇博客展示了如何使用Java的Runtime类执行外部应用程序(如VB),并捕获其标准输出和错误输出。代码中定义了两个方法,分别接受字符串和字符串数组参数来执行外部应用,并将输出结果返回。在测试程序中,调用了指定路径的XraySystem.exe程序。
摘要由CSDN通过智能技术生成

调用外部应用程序(譬如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 阅读(705) 评论(0)  编辑  收藏 所属分类: Java技术

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值