Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > How can I execute a shell command from Java?

How can I execute a shell command from Java?
Thread Tools
Senior User
Join Date: Mar 2001
Status: Offline
Reply With Quote
May 23, 2001, 10:35 AM
 
Hi, I am just getting started with Java, and have hopefully a simple question. How can I execute a shell (command line) command from Java and capture it's output? Can someone lead me in the right direction?

Thanks!
     
Dedicated MacNNer
Join Date: Oct 2000
Location: Pasadena, CA, USA
Status: Offline
Reply With Quote
May 23, 2001, 11:04 AM
 
Here's a simple sample that does more or less what you want:

Runtime r = Runtime.getRuntime();
Process p = r.exec ( "/bin/hostname" );
try {
p.waitFor();
} catch (InterruptedException e){
// do something
}
InputStream in = p.getInputStream();
...

You can also send stuff to the process via stdout, check exit codes, and so on.

     
Senior User
Join Date: Mar 2001
Status: Offline
Reply With Quote
May 26, 2001, 08:20 PM
 
Thanks for the response!


[This message has been edited by iamnotmad (edited 05-26-2001).]
     
   
Thread Tools
Forum Links
Forum 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 On
Top
Privacy Policy
All times are GMT -5. The time now is 03:13 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2