How to send Windows path (With spaces) as parameters to Runtime.exec with Java 1.5?

Edy1988
Edy1988 used Ask the Experts™
on
Hi,

In Java 1.4 when I needed to send to Runtime.exec a parameter with spaces (Path under 'Program Files') I would just wrap it with:

String quotes= "\"";

and put quotes before and after it.

In Java 1.5 it adds to the string \" and not just " as I wanted, so how do I add " at the beginning and at the end?

Thanks
Eyal
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
What is the path
Top Expert 2016

Commented:
You would generally use something like

path = "\"C:\\Program Files\\Something\\Something.exe\"";

Author

Commented:
C:\Program Files\....

Author

Commented:
This path also crash, you need to have:

""C:\\Program Files\\Something\\Something.exe" -version", no?
Top Expert 2016

Commented:
If there are parameters then it would be

path = "\"C:\\Program Files\\Something\\Something.exe -version\"";

Top Expert 2016

Commented:
You'd be better off doing it more like this though:

List<String> args = new ArrayList<String>(2);
 args.add("C:\\Program Files\\Something\\Something.exe");
 args.add("-version");
 ProcessBuilder pb = new ProcessBuilder(args);

Author

Commented:
Tryed the path = "\"C:\\Program Files\\Something\\Something.exe -version\"";
It still cuts the path after the C:\Program

Any ideas?
you probably need to \ the spaces too?
also, try adding file:/

"file:/C:\\gen\\currency\\";

Author

Commented:
Hi,

The problem was the parameters "cmd.exe /C" before the command, now it works.

Thanks
PAQed with points refunded (100)

Computer101
EE Admin

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial