PDA

View Full Version : Opening apps from inside Java Code




Omega Duck
Feb 1, 2009, 10:27 AM
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



toddburch
Feb 1, 2009, 11:38 AM
Sure. Look into the Runtime() and Process() classes.

foidulus
Feb 1, 2009, 12:20 PM
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
Feb 1, 2009, 01:39 PM
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
Feb 4, 2009, 09:08 AM
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