How to talk to the command line from inside a Java app

  • Posted: 10 March 2002 10:16 PM

    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>

         
  • Posted: 14 February 2002 04:28 AM #1

    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

         
  • Posted: 14 February 2002 07:36 AM #2

    Runtime runtime = Runtime.getRuntime();

    and I thought java was supposed to be intelligible? 

     

    Signature

    All statements are to be taken with a grain of salt

         
  • Posted: 10 March 2002 10:16 PM #3

    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..