How to execute a batch file from java?

2020-02-05 03:44发布

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

I am using the following command.

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

But the problem is I want to give a reative path instead of absolute path so that I can deploy that java project on any comp.

The dir structure of the project is like as follows:

com
   |
  project
   |
   ------ parser
   |         |_____ Main.java
   |
   -------util
             |_____ Server.bat

I want to run the "Server.bat" file in the "util" dir from the "Main.java" file in the "parser" dir.

7条回答
时光不老,我们不散
2楼-- · 2020-02-05 04:36

You can try it with Desktop if supported (Java 1.6)

    File file = new File("server.bat");
    Desktop.getDesktop().open(file);
查看更多
登录 后发表回答