Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Runtime.getRuntime().exec() with spaces in command argument

807589Aug 19 2008 — edited Aug 22 2008
Hello.

I know this topic has been discussed in several posts here and there, but my problem has a bit of a twist.

Essentially, I'm trying to call a Windows application that takes several command line arguments. I have to preserve blank spaces within one of the arguments, but it cannot be surrounded with quotes (").

The application I'm attempting to launch (with arguments included) is:

c:\Program Files\AppX\app.exe parm1;parm2;parm3;parm4a parm4b;parm5


So, I've been building String[]s and passing them to

{code}Runtime.getRuntime().exec(cmdArray, null, null);{code}


I've tried dozens of ways of defined the arguments, but haven't come up with any that work. I've had several that work when there are no spaces between parm4a and parm4b, but they all break down when the spaces are included.

Here's a summary of my attempts so far:

{code}
// Case 1: the following works if there are no spaces in the parms:
String[] cmdArray = new String[2];
cmdArray[0] = "c:\\Program Files\\AppX\\app.exe";
cmdArray[1] = "parm1;parm2;parm3;parm4aparm4b;parm5";

// Case 2: if there are spaces in the parms:
String[] cmdArray = new String[2];
cmdArray[0] = "c:\\Program Files\\AppX\\app.exe";
cmdArray[1] = "parm1;parm2;parm3;parm4a parm4b;parm5";
// result: then we get an Invalid Command Line Arguments from AppX
// product, which states that the command line arguments passed in were
// "parm1;parm2;parm3;parm4a parm4b;parm5" (something wrapped it in quotes).

// Case 3: the following works if there are no spaces in the parms:
String[] cmdArray = new String[4];
cmdArray[0] = "cmd";
cmdArray[1] = "/C";
cmdArray[2] = "c:\\Program Files\\AppX\\app.exe";
cmdArray[3] = "parm1;parm2;parm3;parm4aparm4b;parm5";

// Case 4: if there are spaces in the parms:
String[] cmdArray = new String[4];
cmdArray[0] = "cmd";
cmdArray[1] = "/C";
cmdArray[2] = "c:\\Program Files\\AppX\\app.exe";
cmdArray[3] = "parm1;parm2;parm3;parm4a parm4b;parm5";
// result: it runs successfully (no errors) but nothing happens.

// Case 5: this works:
String[] cmdArray = new String[5];
cmdArray[0] = "cmd";
cmdArray[1] = "/C";
cmdArray[2] = "c:\\Program Files\\AppX\\app.exe";
cmdArray[3] = "parm1;parm2;parm3;";
cmdArray[4] = "parm4aparm4b;parm5;";

// Case 6: but this doesn't:
String[] cmdArray = new String[5];
cmdArray[0] = "cmd";
cmdArray[1] = "/C";
cmdArray[2] = "c:\\Program Files\\AppX\\app.exe";
cmdArray[3] = "parm1;parm2;parm3;";
cmdArray[4] = "parm4aparm4b;";
cmdArray[5] = "parm5;";

// Case 7: when this is run, nothing happens:
String[] cmdArray = new String[5];
cmdArray[0] = "cmd";
cmdArray[1] = "/C";
cmdArray[2] = "c:\\Program Files\\AppX\\app.exe";
cmdArray[3] = "parm1;parm2;parm3;";
cmdArray[4] = "parm4a parm4b;parm5;";

// Case 8:
String[] cmdArray = new String[5];
cmdArray[0] = "c:\\Program Files\\AppX\\app.exe";
cmdArray[1] = "parm1;";
cmdArray[2] = "parm2;";
cmdArray[3] = "parm3;";
cmdArray[4] = "parm4a parm4b";
// result: submits parm1;parm2;parm3;"parm4a parm4b" which results in the
// the quotes being submitted as a part of the argument. Note that in this
// case parm5 is not submitted because is it supposedly considered optional

{code}


Anyway, in case there is anyone who read through all that, could decipher my meaning, and has any thoughts or comments, you have my thanks in advance.

Just as a disclaimer, I am aware of the ProcessBuilder class in Java 5, but our existing codebase was using Runtime.getRuntime().exec() so I stuck with that. From what I've read, using the ProcessBuilder won't help me in this case, though I'd love to be wrong.

Thanks again,
Jeff
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 19 2008
Added on Aug 19 2008
31 comments
4,155 views