Java调用外部程序实例(windows批处理篇)

  • public Process exec(String command);
  • public Process exec(String [] cmdArray);
  • public Process exec(String command, String [] envp);
  • public Process exec(String [] cmdArray, String [] envp);

  • java调用外部程序主要使用上述4个函数,具体用法可以查看API文档说明。

    以下代码在jdk1.5版经过测试通过。

     

    java代码:

    1. import java.io.BufferedReader;
    2. import java.io.IOException;
    3. import java.io.InputStream;
    4. import java.io.InputStreamReader;
    5. public class WindowsExec {
    6.     
    7.     public static void main(String args[]) {
    8.         if (args.length < 1) {
    9.             System.out.println("USAGE: java  WindowsExec  <cmd>");
    10.             System.exit(1);
    11.         }
    12.         try {
    13.             String osName = System.getProperty("os.name");
    14.             String[] cmd = new String[4];
    15.             System.out.println(osName);
    16.             if (osName.equals("Windows XP")) {
    17.                 cmd[0] = "cmd.exe";
    18.                 cmd[1] = "/C";
    19.                 cmd[2] = args[0]; // *.bat
    20.                 cmd[3] = "20080612";// parameter
    21.             } else {
    22.                 //unix
    23.             }
    24.             Runtime rt = Runtime.getRuntime();
    25.             System.out.println("Execing " + cmd[0] + " " + cmd[1] + " "+ cmd[2] + cmd[3]);
    26.             Process proc = rt.exec(cmd);
    27.             // get error stream
    28.             StreamGobbler errorGobbler 
    29.                 = new StreamGobbler(proc.getErrorStream(), "ERROR");
    30.             // get input stream
    31.             StreamGobbler outputGobbler 
    32.                 = new StreamGobbler(proc.getInputStream(), "OUTPUT");
    33.             // output msg
    34.             errorGobbler.start();
    35.             outputGobbler.start();
    36.             // return value
    37.             int exitVal = proc.exitValue();
    38.             System.out.println("ExitValue:   " + exitVal);
    39.         } catch (Throwable t) {
    40.             t.printStackTrace();
    41.         }
    42.     }
    43. }
    44. class StreamGobbler extends Thread {
    45.     InputStream is;
    46.     String type;
    47.     StreamGobbler(InputStream is, String type) {
    48.         this.is = is;
    49.         this.type = type;
    50.     }
    51.     public void run() {
    52.         try {
    53.             InputStreamReader isr = new InputStreamReader(is);
    54.             BufferedReader br = new BufferedReader(isr);
    55.             String line = null;
    56.             while ((line = br.readLine()) != null){
    57.                 System.out.println(type + ">" + line);
    58.             }
    59.         } catch (IOException ioe) {
    60.             ioe.printStackTrace();
    61.         }
    62.     }
    63. }

     

    batch代码:

    1. echo off
    2. set PATH=%JAVA_HOME%bin;%PATH%
    3. d:
    4. set basedir=D:/batch
    5. echo 批处理: %1
    6. java -jar %basedir%/batch.jar %1
    7. IF ERRORLEVEL 1 goto ERROR_EXIT
    8. echo ***** 正常終了 *****
    9. EXIT 0
    10. :ERROR_EXIT
    11. echo ***** 发生错误 *****
    12. EXIT 1

    完~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值