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.
解决方案
When Java is running and you use Runtime.exec() with a relative path, relative means relative to the current user direcory, where the JVM was invoked.
This may work
Runtime.getRuntime().exec("cmd.exe", "/c", "./com/projct/util/server.bat");
if you start java from com's parent directory.
Or you must calculate an absolut path:
Runtime.getRuntime().exec("cmd.exe", "/c",
System.getProperty("user.dir")+"/com/projct/util/server.bat");