在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
替换为实际的批处理文件路径。运行以上代码将调用批处理文件并打印出其输出。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:Windows批处理文件怎么运行