如何执行从Java批处理文件? [英] How to execute a batch file from java?
问题描述
我想从一个java程序执行批处理文件。
I want to execute a batch file from a java program.
我使用下面的命令。
Runtime.getRuntime().exec("server.bat");
但问题是我想给一个reative路径,而不是绝对路径,这样我可以部署在任何补偿的Java项目。
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
我要运行Server.bat中的UTILDIR从Main.java文件,文件中的语法分析器DIR。
I want to run the "Server.bat" file in the "util" dir from the "Main.java" file in the "parser" dir.
推荐答案
在运行Java和您使用的Runtime.exec()使用相对路径,相对于当前用户的猪病,其中JVM被调用相对的手段。
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.
这可能工作
Runtime.getRuntime().exec("cmd.exe", "/c", "./com/projct/util/server.bat");
如果你开始从COM的父目录的java。
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");
我忘了,请参阅运行时.exec()将不会的。
这篇关于如何执行从Java批处理文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!