i am creating a java application which is will be GUI for a bash script..

The application will be used browse the script location and enter 2 other parameters..

and build and execute the command..

so the execute part will be like this:

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(scriptPath + parameter1 + parameter2);

if the command work if the script's location doesn't has spaces in it..

but when it has spaces it stops when it reach the first space.. and it gives me "java.io.IOException:" exeption..that the file is not found..

i have tried replacing the spaces with backspace and space..

scriptPath.replace(" ", "\\ ")

i also tried adding quotes before and after the path

"\"" + scriptPath + "\""

and i have tried them both together..

"\"" + scriptPath.replace(" ", "\\ ") + "\""

but none of them had worked..

any help will be appreciated..

regards..

link|improve this question
feedback

2 Answers

Use ProcessBuilder instead. It has a constructor that lets you add the parameters as separate strings, and should handle spaces.

link|improve this answer
feedback

Runtime's exec(String) method just splits the string on whitespace; it doesn't understand any sort of quoting. Instead, you can use its exec(String[]) method, which takes an array, so doesn't have to apply any splitting logic:

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(new String[] { scriptPath, parameter1, parameter2 });
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.