要从Java应用程序运行批处理文件,您可以使用以下方法:
public class RunBatchFile {
public static void main(String[] args) {
try {
Runtime.getRuntime().exec("cmd /c start yourBatchFile.bat");
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.IOException;
public class RunBatchFile {
public static void main(String[] args) {
ProcessBuilder processBuilder = new ProcessBuilder("yourBatchFile.bat");
processBuilder.redirectErrorStream(true);
try {
Process process = processBuilder.start();
int exitCode = process.waitFor();
System.out.println("\nExited with error code : " + exitCode);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
这两种方法都可以用来运行批处理文件。第一种方法使用Runtime类,第二种方法使用ProcessBuilder类。您可以根据自己的需求选择其中一种方法来运行批处理文件。
云+社区开发者大会 长沙站
Elastic 实战工作坊
Elastic 实战工作坊
云+社区技术沙龙 [第30期]
Elastic 中国开发者大会
北极星训练营
云+社区技术沙龙[第17期]
高校公开课
DBTalk
领取专属 10元无门槛券
手把手带您无忧上云