Opening apps from inside Java Code

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

  1. macrumors newbie

    Jan 30, 2009
    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

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

    Jan 15, 2007
    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/";
    Process child = Runtime.getRuntime().exec(command);

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


    Jan 10, 2005
    Dallas, TX
    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.

  5. thread starter macrumors newbie

    Jan 30, 2009

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

    or am i totally missing something?

    thx a lot for the replies

Share This Page