Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Omega Duck

macrumors newbie
Original poster
Jan 30, 2009
4
0
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
 

foidulus

macrumors 6502a
Jan 15, 2007
904
1
Sure. Look into the Runtime() and Process() classes.

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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
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.

-Lee
 

Omega Duck

macrumors newbie
Original poster
Jan 30, 2009
4
0
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.


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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.