• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Maulin,
Many thanks for your demo code. It solves my problem.
Rgs,
lixu
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic