Java调用exe带参数详解

1. 引言

在Java开发过程中,有时需要调用外部的可执行程序(exe)来完成一些特定的功能。而调用这些可执行程序时,往往需要传递一些参数给它们。本文将介绍如何在Java中调用带参数的可执行程序,以及如何处理参数的传递和返回结果。

2. 基本概念

在Java中调用可执行程序,可以使用Runtime类或ProcessBuilder类。通常情况下,使用ProcessBuilder类更加灵活和方便。

  • Runtime类:代表运行时环境,可以通过调用其exec()方法执行外部命令,并返回一个Process对象,用于控制该进程的输入、输出和错误流。
  • ProcessBuilder类:用于创建进程,并执行指定的命令。可以通过调用其command()方法设置命令和参数,并返回一个Process对象,用于控制该进程的输入、输出和错误流。

3. 调用可执行程序

下面以调用一个名为example.exe的可执行程序为例,该程序需要接收一个参数。

使用Runtime

使用Runtime类调用可执行程序的步骤如下:

  1. 获取Runtime对象。
  2. 调用exec()方法执行可执行程序,并传入命令和参数。
import java.io.IOException;

public class RuntimeExample {
    public static void main(String[] args) {
        try {
            // 获取Runtime对象
            Runtime runtime = Runtime.getRuntime();
            // 执行可执行程序
            Process process = runtime.exec("example.exe arg1");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

使用ProcessBuilder

使用ProcessBuilder类调用可执行程序的步骤如下:

  1. 创建一个ProcessBuilder对象,使用command()方法设置命令和参数。
  2. 调用start()方法执行可执行程序。
import java.io.IOException;

public class ProcessBuilderExample {
    public static void main(String[] args) {
        try {
            // 创建ProcessBuilder对象
            ProcessBuilder pb = new ProcessBuilder("example.exe", "arg1");
            // 执行可执行程序
            Process process = pb.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

4. 传递参数和接收结果

在调用可执行程序时,可以通过命令行传递参数给它们。可执行程序可以通过命令行参数来接收这些参数。

传递参数

在使用Runtime类和ProcessBuilder类调用可执行程序时,可以通过在命令中添加参数来传递给它们。

import java.io.IOException;

public class ParameterExample {
    public static void main(String[] args) {
        try {
            // 获取Runtime对象
            Runtime runtime = Runtime.getRuntime();
            // 执行可执行程序并传递参数
            Process process = runtime.exec("example.exe arg1 arg2");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
import java.io.IOException;

public class ParameterExample {
    public static void main(String[] args) {
        try {
            // 创建ProcessBuilder对象
            ProcessBuilder pb = new ProcessBuilder("example.exe", "arg1", "arg2");
            // 执行可执行程序
            Process process = pb.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

接收结果

在调用可执行程序后,可以通过Process对象获取它的输出流和错误流,以及等待它执行完毕。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ResultExample {
    public static void main(String[] args) {
        try {
            // 获取Runtime对象
            Runtime runtime = Runtime.getRuntime();
            // 执行可执行程序并传递参数
            Process process = runtime.exec("example.exe arg1");
            
            // 获取输出流
            BufferedReader outputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = outputReader.readLine()) != null) {
                System.out.println(line);
            }
            outputReader.close();
            
            // 获取错误流
            BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            while ((line = errorReader.readLine()) != null) {
                System.err.println(line);
            }
            errorReader.close();
            
            // 等待可执行程序执行完毕