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 / November 2006

Tip: Looking for answers? Try searching our database.

How do I call a system (shell) app ?

Thread view: 
me2 - 05 Nov 2006 02:43 GMT
I'm writing a Java app that needs to call a shell command.  How do I do it
and how do I get the return value and the information that comes back from
the call ?

Lets say I needed to call "ls | grep somefile".  That ISN'T what I need to
do, but lets say it was.  How do I get the result of this call ?  How do I
get the return value ?

In case someone needs to know how this can possibly be portable, the call
is to ufraw, which is available for Linux, Windows and Mac.

Thanks
me2 - 05 Nov 2006 02:51 GMT
Its called with:

Runtime.getRuntime().exec("ls | grep");

But how do I get the return stuff ?
Gordon Beaton - 05 Nov 2006 09:16 GMT
> Runtime.getRuntime().exec("ls | grep");
>
> But how do I get the return stuff ?

Start by reading the API documentation for Runtime.exec(), and note
that there is a return value:

 Process p = Runtime.exec(...);

Get the exit value from the Process:

 p.exitValue();

However the example you've given will most definitely *not* work as
you expect because of the pipe character. Pipes are a shell feature,
but Runtime.exec() does not use a shell to run the command. If you
want to use pipes, redirection or other shell features you must run a
shell yourself. You also need to group the arguments correctly for the
shell, e.g.:

 String[] cmd = { "/bin/sh", "-c", "ls | grep foo" };
 Process p = Runtime.exec(cmd);

/gordon

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

me2 - 05 Nov 2006 17:57 GMT
>> Runtime.getRuntime().exec("ls | grep");
>>
[quoted text clipped - 20 lines]
>
> /gordon

Excellent reply.  Thanks !  Much appreciated.

Next question... is an exec call blocking ?  Ie does the current thread
hold up until the command is done ?  It seems to the way you use p to
access the reply like that.
Gordon Beaton - 05 Nov 2006 18:22 GMT
> Next question... is an exec call blocking ? Ie does the current
> thread hold up until the command is done ? It seems to the way you
> use p to access the reply like that.

No, Runtime.exec() returns after starting the child, which then runs
separate from the caller. Between calling exec() and p.exitValue() you
must read from (both!) stdout and stderr of the child until EOF.

/gordon

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e



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.