【发布时间】:2017-05-17 21:50:17
【问题描述】:
我正在开发一个 Java 应用程序,其任务之一是使用 Runtime exec 执行系统命令。该命令需要另一个 JAR 文件才能工作,但每当执行命令的部分时,我都会收到“错误:无法访问 jarfile”。
首先,我当前的工作目录:
assets/jarFile.jar
myApp.jar
我的代码如下所示:
try {
// To get the path of the directory where my current running JAR is
String jarPath = myApp.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String path = URLDecoder.decode(jarPath.substring(0, jarPath.lastIndexOf("/")), "UTF-8");
// The system command to execute
String command = "java -Xmx1024m -jar "+path+"/assets/jarFile.jar and-so-on...";
Process p = Runtime.getRuntime().exec(command);
...
}
顺便说一下,jarFile.jar 不是可执行的 JAR。
有什么想法吗?
【问题讨论】:
-
@NickL 在发布我的问题之前,我也已经阅读了该问题及其答案。我们处于不同的情况。不可能重复。
-
好吧,那你试过什么?位置不同?路径中的斜线错误?奇怪的符号?空间?你试过用引号括起来吗?在 exec 中手动输入完整路径?检查权限了吗?
-
@NickL 是的,我尝试了直接路径
String command = "java -Xmx1024m -jar D:/assets/jarFile.jar and-so-on...";仍然一样... -
从终端运行时也会发生同样的情况?