jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes Runtime.getRuntime().exec(s) spaces in s are trimed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Runtime.getRuntime().exec(s) spaces in s are trimed" Watch "Runtime.getRuntime().exec(s) spaces in s are trimed" New topic
Author

Runtime.getRuntime().exec(s) spaces in s are trimed

Xu Li
Greenhorn

Joined: Dec 02, 2002
Posts: 11
I use Runtime.getRuntime().exec(s). s is passed as "cmd /c c:\\test.exe \"1 2\"", but the args[1] in the main of test.exe is changed to "1 2". What should I do to keep the space in the parameters that is essential. Pls. advice. Thx. a lot.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi Xu,
u can use,
Process exec(String[] cmdarray)

method of Runtime instead of simple exec(String command) as per the API...
here this string array has the following format,
String[]{'command','arg1','arg2','arg3',...}
so this way you can separate arguments...
now u can use,
String[]{'command',"1 2"} in your case which should solve ur problem.
hope i got your question correctly. let me know if i've misunderstood ur question...
please look at the
API
for more details...
regards
maulin
Michael Zalewski
Ranch Hand

Joined: Apr 23, 2002
Posts: 168
I bet it also works if you take out the cmd /c.
My guess is cmd sees the argument with the spaces. So it sees the arguments "/c", "c:\test.exe", and "1 2". It does its thing (specified by "/c", and builds the command "c:\test.exe 1 2".
If you really need to use cmd /c, try triple quoting. "cmd /c c:\\test.exe \"\"\"1 2\"\"\"". Now, cmd should see the arguments '/c', 'c:\test.exe', and '"1 2"'. So it should execute the command 'c:\test.exe "1 2"', which is what you want (I think).
Xu Li
Greenhorn

Joined: Dec 02, 2002
Posts: 11
Hi,
Thanks a lot for both of ur replies. I am really sorry that I did not put it clearly in my orginal message. I mean I want to pass "1 2" with 4 whitespace in between 1 and 2 but within the test.exe, there is only one whitespace left. However, the number of whitespaces is essential to me. Please let know if u know how to solve the problem. Thanks a lot.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi,
i guess if u use the option i suggested then it should work fine for you.
try it and let me know if it works for you.
i tried the following code,

here is mybatchfile.bat,

here is the output,
------------------------
C:\maulin>java process
p.wait() called
p.wait() returned
Reading process output
C:\maulin>echo off
"First Argument:" "1 2"
"Second Argument:" 3
--------------------------
it preserved spaces for my batch file.
regards
maulin
Xu Li
Greenhorn

Joined: Dec 02, 2002
Posts: 11
Hi Maulin,
Many thanks for your demo code. It solves my problem.
Rgs,
lixu
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Runtime.getRuntime().exec(s) spaces in s are trimed