Opening apps from inside Java Code

Discussion in 'Mac Programming' started by Omega Duck, Feb 1, 2009.

  1. macrumors newbie

    Joined:
    Jan 30, 2009
    #1
    I'm having a little problem with Java.
    I am quite new to Java and therefore i am just trying things out.
    And i wanted to ask if it is possible to launch an application (e.g. firefox) from the code. Like when i run the code, and i press something (e.g. a JButton) that the applications opens?

    thx a lot
     
  2. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #2
    Sure. Look into the Runtime() and Process() classes.
     
  3. macrumors 6502a

    Joined:
    Jan 15, 2007
    #3
    That would be how to do it, and there are tons of examples out there. You should execute the "open" command("/usr/bin/open") to launch the App.

    For example, to open Firefox, use the following:

    String command="/usr/bin/open /Applications/Firefox.app";
    Process child = Runtime.getRuntime().exec(command);

    While the full path isn't strictly necessary, I prefer to use it.
     
  4. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    Also, beware of your pathing. Java is supposed to be portable... so be aware if this app is to be used on more than OS X.

    -Lee
     
  5. thread starter macrumors newbie

    Joined:
    Jan 30, 2009
    #5

    So with an "if statement" would it look something around this:
    if (event.getSource() == nItem) {
    System.open(String command="/usr/bin/open /Applications/Firefox.app");
    Process child = Runtime.getRuntime().exec(command));
    }

    or am i totally missing something?

    thx a lot for the replies
     

Share This Page