How to execute a batch file from java?

前端 未结 7 466
逝去的感伤
逝去的感伤 2020-12-09 20:42

I want to execute a batch file from a java program.

I am using the following command.

Runtime.getRuntime().exec(\"server.bat\");

Bu

相关标签:
7条回答
  • 2020-12-09 21:13

    You can use ProcessBuilder for this. It provides much more control than exec. Particularly, it allows to set working directory with method directory.

    Example:

    ProcessBuilder pb = new ProcessBuilder("server.bat");
    pb.directory(new File(deployDir + "\\com\\project\\util"));
    Process p = pb.start();
    int exitStatus = p.waitFor();
    

    Of course, your app must get deployDir from somewhere. It can be set in environment, in application configuration file, it can be current user directory or anything else.

    0 讨论(0)
提交回复
热议问题