简体   繁体   English

尝试将外部jar作为进程执行时无法访问jar文件

[英]Unable to access jar file when trying to execute external jar as Process

Well netbeans Java 7u17 project. 好的netbeans Java 7u17项目。 I'm trying to execute an external jar file that has dependencies on the following way: 我正在尝试执行一个依赖于以下方式的外部jar文件:

ProcessBuilder pb = new ProcessBuilder(new String[]{"java", "-jar", String.format("%s %s", path, cmde)});
pb.redirectErrorStream(true);
Process proc = pb.start();

And when I execute this application I get the following error message: 当我执行此应用程序时,出现以下错误消息:

Error: Unable to access jarfile C:/Users/XXXX/Documents/SampleApplication/ProcessTest/proc.jar -k

The jar file has its depenedcies (other jar files) right next to it in a folder called lib. jar文件在其旁边的名为lib的文件夹中具有其依赖关系(其他jar文件)。 When I open up PowerShell or Command Prompt and manually enter 当我打开PowerShell或命令提示符并手动输入时

java -jar C:/Users/XXXX/Documents/SampleApplication/ProcessTest/proc.jar -k

The application is executed flawlessly. 该应用程序可以完美执行。 Why not from my java application? 为什么不从我的Java应用程序呢? I tried numerous answers for similar problem from stack overflow but none of them worked. 我从堆栈溢出尝试了许多类似问题的答案,但是没有一个起作用。 Some example: - Restarted netbeans - Closed & Reopened project - Rebuilt proc.jar 例如:-重新启动netbeans-已关闭和重新打开的项目-重新构建了proc.jar

thanks for every help - Joey 谢谢你的帮助-乔伊

I could not solve it neither, but I have a workaround. 我也无法解决,但有解决方法。
Create a bat script which runs your jar and execute that bat file like: 创建一个运行您的jar的bat脚本并执行该bat文件,如下所示:

ProcessBuilder pb = new ProcessBuilder("c:\\...\\runproc.bat");

EDIT: 编辑:

You need to split the command arguments one by one, like: 您需要将命令参数一一拆分,例如:

ProcessBuilder("java", "-jar", path, arg1, arg2, ...)

Sorry for the answer, I can't make comments yet, but maybe you should use: 抱歉,我无法发表评论,但也许您应该使用:

//This is correct
String path = "C:\\xxx\\xxx";
//This is incorrect
String path = "C:/xxx/xxx";

I'm just assuming, since your output shows "/" instead of the windows standard "\\". 我只是假设,因为您的输出显示为“ /”,而不是Windows标准的“ \\”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM