java怎么调用批处理文件

   2025-02-13 3160
核心提示:在Java中,可以使用Runtime类的exec()方法执行批处理文件。以下是一个简单的示例:public class CallBatchFile {public static v

在Java中,可以使用Runtime类的exec()方法执行批处理文件。

以下是一个简单的示例:

public class CallBatchFile {    public static void main(String[] args) {        try {            // 调用批处理文件            Process process = Runtime.getRuntime().exec("path_to_batch_file.bat");            // 获取批处理文件的输出            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));            String line;            while ((line = reader.readLine()) != null) {                System.out.println(line);            }            // 等待批处理文件执行完毕            int exitCode = process.waitFor();            System.out.println("Exit Code: " + exitCode);        } catch (IOException | InterruptedException e) {            e.printStackTrace();        }    }}

请将path_to_batch_file.bat替换为实际的批处理文件路径。运行以上代码将调用批处理文件并打印出其输出。

 
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  网站留言