I get this error,

Exception in thread "main" java.io.IOException: Cannot run program "java -jar C:\Users\Jack\testFile.jar\" (in directory "\"): CreateProcess error=2, The system cannot find the file specified
	at java.lang.ProcessBuilder.start(Unknown Source)
	at testProject.Main.main(Main.java:15)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
	at java.lang.ProcessImpl.create(Native Method)
	at java.lang.ProcessImpl.<init>(Unknown Source)
	at java.lang.ProcessImpl.start(Unknown Source)
	... 2 more

when trying to use this code:

ProcessBuilder pbT = new ProcessBuilder("java -jar "+files[0]);;
		pbT.directory(new File("/"));
		pbT.start();

That's just in my main() method

Does anyone have any insight as to why, and if so how I can fix it?

Thanks!
-Jack

Oh, I see. Well, from the error I can see that you are trying to access a file that doesn't exist. You should catch that exception.

What are you trying to do with it? You can get an IOException when you can't open/edit your file. Also, starting with a slash means you take the absolute path.

The file is a jarfile, and I want to run it. The process builder returns Error=2 now. I really don't know quite what's wrong as the command works perfectly from command prompt.

IOException: Cannot run program "java -jar C:\Users\Jack\testFile.jar\"

That final \ on the filename looks wrong to me. Very wrong.

Really?
If I used:

if(files[0].endswith("\\")
Somemethodtoremovelastchar();

Do you think I would have better luck?

Well, does the command with \ at the end work in the prompt? Testing helps usually, just try and check if it works :)

EDIT: You just posted before me. I would suggest you try what JamesCherill said.

Edited 5 Years Ago by hiddepolen: n/a

This article has been dead for over six months. Start a new discussion instead.