简体   繁体   中英

Unable to access jar file when trying to execute external jar as Process

Well netbeans Java 7u17 project. I'm trying to execute an external jar file that has dependencies on the following way:

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. When I open up PowerShell or Command Prompt and manually enter

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

The application is executed flawlessly. Why not from my java application? 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

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:

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 "\\".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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