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>



11” MacBook Air 1.6GHz dual-core Intel Core i5: $829.00 Delivered
Samsung S22B300B 21.5” LED Backlit LCD Monitor: $129.99 Delivered
Canon imageCLASS Monochrome Multifunction Laser Printer: $129.99 Delivered
