The Mac Observer

 
   
 
How to talk to the command line from inside a Java app
Posted: 10 March 2002 09:16 PM [ Ignore ]
stars_5
Total Posts:  1118
Joined  2001-07-05

Okay,

So this took me absolutely forever to figure out (seriously, I’ve been at this for at least 8 hours solid) - but I’ve finally got it figured out.

The question: How do you talk to the command line from inside a Java application and see the result in a Terminal window?

Answer:

Runtime runtime Runtime.getRuntime();
String cmd "osascript -e 'tell application "Terminal"' 
    -e 'activate' 
    -e 'do script with command "
[enter command here]"'
    -e 'end tell'"
;
Process child runtime.exec(new String[] {"/bin/tcsh","-c"cmd}); 

How does it work? Well, first, you have to get your application’s runtime object, and then tell it to execute a new shell application. This shell application is told to execute the command ‘cmd’ immediately, and the ‘cmd’ is actually a call to osascript (see ‘man osascript’ for more info) that cobbles together an applescript that runs on Terminal and tells it to execute the command of your choice.

I have wasted an entire day trying to figure this out. I have scoured the web in search of information on using Runtime.exec() only to be told time and time again that it is (a) a pain in the butt and (b) extremely platform specific, and how it works depends on what platform you’re on. On top of that, Terminal.app isn’t the easiest program to talk to (compared to, say, xterm). To make an extremely long story short, there is absolutely no information out there that’ll tell you how to make your Java application talk to your Mac OS X command line - until now.

Anyway, now that I have it all figured out, I give it to you, my friends, to use at your leisure so that you don’t have to go through all the pain I did. I’ll edit this message when I manage to paste together a class that’ll make the process even easier, at which time I will post said source somewhere.

There. My work is done for today.

BTW - If anyone has a better way to do this than my current (runtime -> shell -> applescript -> terminal -> command) method, please post it - as functional as the current implementation is, it’s still a bit of a mess.

[edited for silly line encodings]

<font size=-1>[ This Message was edited by: Morto on 2002-02-14 04:09 ]</font>

Profile
 
 
Posted: 14 February 2002 03:28 AM [ Ignore ] [ # 1 ]
Moderator
Total Posts:  3912
Joined  2001-07-13

I have not attempted this, and I cannot offer a better way, but I salute you! [Raises cup of Italian Roast in respect]

 

 Signature 

Work is the curse of the drinking classes.
- Oscar Wilde

Profile
 
 
Posted: 14 February 2002 06:36 AM [ Ignore ] [ # 2 ]
stars_5
Total Posts:  1162
Joined  2001-11-02

Runtime runtime = Runtime.getRuntime();

and I thought java was supposed to be intelligible?  icon_lol.gif

 

 Signature 

All statements are to be taken with a grain of salt

Profile
 
 
Posted: 10 March 2002 09:16 PM [ Ignore ] [ # 3 ]
stars_5
Total Posts:  1118
Joined  2001-07-05

On 2002-02-14 04:03, Morto wrote:
I’ll edit this message when I manage to paste together a class that’ll make the process even easier, at which time I will post said source somewhere.

And you can find the source here at my homepage. Just pick download Shell.java. You could also look at my cute bunny if you were so inclined, but I suspect that’s not what you’d be looking for.. icon_smile.gif

Profile
 
 
   
 
 

Apple Stock Quote (AAPL)

Loading...

Hot Topics

TMO Express

Join the TMO Express Daily Newsletter to get the latest Mac headlines in your e-mail every weekday. Find out more!

Top Deals From DealBrothers.com

Recent Features

Support The Mac Observer

We noticed you may be running AdBlock on your computer. It takes real money to run this site and to deliver the news, tips, and opinions you love to read.

If you wish to block the ads that pay for the creation of our content, we ask that you instead support TMO Directly, either with a $5 monthly recurring contribution, or a one-time donation of any amount of your choice. Thanks!

Subscribe with Paypal Donate with Paypal