1

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

2 Answers 2

2

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 });
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.