Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / July 2007

Tip: Looking for answers? Try searching our database.

Runtime.exec help please

Thread view: 
Steve Rainbird - 25 Jul 2007 14:56 GMT
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.

Signature

Steve

Daniel Pitts - 25 Jul 2007 15:23 GMT
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.
>
[quoted text clipped - 38 lines]
> --
> 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.
Steve Rainbird - 25 Jul 2007 15:29 GMT
> On Jul 25, 6:56 am, "Steve Rainbird"
> <news.nos...@rainbird.me.nospam.uk> wrote:
[quoted text clipped - 45 lines]
> 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.

Signature

Steve

Steve Rainbird - 25 Jul 2007 15:42 GMT
>> On Jul 25, 6:56 am, "Steve Rainbird"
>> <news.nos...@rainbird.me.nospam.uk> wrote:
[quoted text clipped - 73 lines]
>
> I will look into ProcessBuilder thanks.

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 =

Signature

Steve

bugbear - 25 Jul 2007 16:13 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.