Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Runtime.exec help please

Reply
Thread Tools

Runtime.exec help please

 
 
Steve Rainbird
Guest
Posts: n/a
 
      07-25-2007
I am trying to use Runtime.exec to run a command and pass it a parameter.

I need it to preserve the spaces in the parameter but cannot get it to work.

E.g

My code says

String cmd = "sh1 xxxx 1234";
Process child = Runtime.getRuntime().exec(cmd);

the sh1 bash shell says

echo '$@' = $@
echo '$1' = $1
echo '$2' = $2

When I run it it displays

$@ = xxxx 1234
$1 = xxxx
$2 = 1234

I want it to pass the code "as is" so as one parameter

So it would display

$@ = xxxx 1234
$1 = xxxx 1234
$2 =

I could even cope with

$@ = xxxx 1234
$1 = xxxx
$2 = 1234


I have tried different variation of single and double quotes without any
success.

--
Steve



 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      07-25-2007
On Jul 25, 6:56 am, "Steve Rainbird"
<news.nos...@rainbird.me.nospam.uk> wrote:
> I am trying to use Runtime.exec to run a command and pass it a parameter.
>
> I need it to preserve the spaces in the parameter but cannot get it to work.
>
> E.g
>
> My code says
>
> String cmd = "sh1 xxxx 1234";
> Process child = Runtime.getRuntime().exec(cmd);
>
> the sh1 bash shell says
>
> echo '$@' = $@
> echo '$1' = $1
> echo '$2' = $2
>
> When I run it it displays
>
> $@ = xxxx 1234
> $1 = xxxx
> $2 = 1234
>
> I want it to pass the code "as is" so as one parameter
>
> So it would display
>
> $@ = xxxx 1234
> $1 = xxxx 1234
> $2 =
>
> I could even cope with
>
> $@ = xxxx 1234
> $1 = xxxx
> $2 = 1234
>
> I have tried different variation of single and double quotes without any
> success.
>
> --
> Steve


did you try:
String command = "sh1 \"first second\"";
Runtime.getRuntime().exec(command);

Alternatively, look into the the ProcessBuilder class, it gives you
more control over individual parameters. Its also more secure if
you're getting data from the user. Not completely secure mind you,
but it does make sure that arguments are escaped appropriately for
your shell.

 
Reply With Quote
 
 
 
 
Steve Rainbird
Guest
Posts: n/a
 
      07-25-2007
"Daniel Pitts" <> wrote in message
news: oups.com...
> On Jul 25, 6:56 am, "Steve Rainbird"
> <news.nos...@rainbird.me.nospam.uk> wrote:
>> I am trying to use Runtime.exec to run a command and pass it a parameter.
>>
>> I need it to preserve the spaces in the parameter but cannot get it to
>> work.
>>
>> E.g
>>
>> My code says
>>
>> String cmd = "sh1 xxxx 1234";
>> Process child = Runtime.getRuntime().exec(cmd);
>>
>> the sh1 bash shell says
>>
>> echo '$@' = $@
>> echo '$1' = $1
>> echo '$2' = $2
>>
>> When I run it it displays
>>
>> $@ = xxxx 1234
>> $1 = xxxx
>> $2 = 1234
>>
>> I want it to pass the code "as is" so as one parameter
>>
>> So it would display
>>
>> $@ = xxxx 1234
>> $1 = xxxx 1234
>> $2 =
>>
>> I could even cope with
>>
>> $@ = xxxx 1234
>> $1 = xxxx
>> $2 = 1234
>>
>> I have tried different variation of single and double quotes without any
>> success.
>>
>> --
>> Steve

>
> did you try:
> String command = "sh1 \"first second\"";
> Runtime.getRuntime().exec(command);


Yep tried that.

I got

$@ = "xxxx 1234"
$1 = "xxxx
$2 = 1234"


BTW I have unset IFS in the shell

so if I just execute

sh1 "xxxxxx 1234"
it does exaclty what I need

$@ = xxxxxx 1234
$1 = xxxxxx 1234
$2 =

>
> Alternatively, look into the the ProcessBuilder class, it gives you
> more control over individual parameters. Its also more secure if
> you're getting data from the user. Not completely secure mind you,
> but it does make sure that arguments are escaped appropriately for
> your shell.
>


I will look into ProcessBuilder thanks.

--
Steve



 
Reply With Quote
 
Steve Rainbird
Guest
Posts: n/a
 
      07-25-2007
"Steve Rainbird" <> wrote in message
news:...
> "Daniel Pitts" <> wrote in message
> news: oups.com...
>> On Jul 25, 6:56 am, "Steve Rainbird"
>> <news.nos...@rainbird.me.nospam.uk> wrote:
>>> I am trying to use Runtime.exec to run a command and pass it a
>>> parameter.
>>>
>>> I need it to preserve the spaces in the parameter but cannot get it to
>>> work.
>>>
>>> E.g
>>>
>>> My code says
>>>
>>> String cmd = "sh1 xxxx 1234";
>>> Process child = Runtime.getRuntime().exec(cmd);
>>>
>>> the sh1 bash shell says
>>>
>>> echo '$@' = $@
>>> echo '$1' = $1
>>> echo '$2' = $2
>>>
>>> When I run it it displays
>>>
>>> $@ = xxxx 1234
>>> $1 = xxxx
>>> $2 = 1234
>>>
>>> I want it to pass the code "as is" so as one parameter
>>>
>>> So it would display
>>>
>>> $@ = xxxx 1234
>>> $1 = xxxx 1234
>>> $2 =
>>>
>>> I could even cope with
>>>
>>> $@ = xxxx 1234
>>> $1 = xxxx
>>> $2 = 1234
>>>
>>> I have tried different variation of single and double quotes without any
>>> success.
>>>
>>> --
>>> Steve

>>
>> did you try:
>> String command = "sh1 \"first second\"";
>> Runtime.getRuntime().exec(command);

>
> Yep tried that.
>
> I got
>
> $@ = "xxxx 1234"
> $1 = "xxxx
> $2 = 1234"
>
>
> BTW I have unset IFS in the shell
>
> so if I just execute
>
> sh1 "xxxxxx 1234"
> it does exaclty what I need
>
> $@ = xxxxxx 1234
> $1 = xxxxxx 1234
> $2 =
>
>>
>> Alternatively, look into the the ProcessBuilder class, it gives you
>> more control over individual parameters. Its also more secure if
>> you're getting data from the user. Not completely secure mind you,
>> but it does make sure that arguments are escaped appropriately for
>> your shell.
>>

>
> I will look into ProcessBuilder thanks.
>
> --
> Steve
>
>
>




ProcessBuilder seems to be the solution thanks.

String cmd = "sh1";
String arg = "xxxx 1234";
Process child = new ProcessBuilder(cmd,arg).start();

$@ = "xxxx 1234"
$1 = "xxxx 1234"
$2 =

--
Steve



 
Reply With Quote
 
bugbear
Guest
Posts: n/a
 
      07-25-2007
Steve Rainbird wrote:
> I am trying to use Runtime.exec to run a command and pass it a parameter.
>
> I need it to preserve the spaces in the parameter but cannot get it to work.


Consider "the other" entry point:
public Process exec(String[] cmdarray)
throws IOException

The parameters are "nicely separate" in the array,
as opposed to the entry point you're using:

public Process exec(String command)
throws IOException

I think what's causing your trouble is this behaviour:

>>The command argument is parsed into tokens
>>and then executed as a command in a separate process.


Reading documentation is sometimes helpful.

BugBear
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please please please help this guy with his open source java app casioculture@gmail.com Java 4 05-05-2005 08:24 AM
Console profile for Windows app in VC++ - PLEASE PLEASE PLEASE HELP! MuZZy C++ 7 01-07-2005 08:40 PM
Computer problems please please please help Nick Computer Support 0 06-04-2004 08:49 PM
HELP! HELP! PLEASE, PLEASE, PLEASE tpg comcntr Computer Support 11 02-15-2004 06:22 PM
please help... ...me learn C++ please please please :) KK C++ 2 10-14-2003 02:08 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57