File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Mac OS and the fly likes launching Safari from a Java App Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » Mac OS
Bookmark "launching Safari from a Java App" Watch "launching Safari from a Java App" New topic

launching Safari from a Java App

Ravi Sathish
Ranch Hand

Joined: Feb 26, 2002
Posts: 131

I'm trying to open a URL from my Java App using Safari...

Here's my code which doesn't throw any exception.. But its not launching Safari though...

When I issue the same command from command line it works alrite:
open -a /Applications/

I would appreciate any solutions,suggestions, and workarounds

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
You might need to escape any spaces in your parameters. Less troublesome would be to separate each space-separated part of the command line into its own string, like "open", "-a", "/...", "http..."
Simon Brown
sharp shooter, and author
Ranch Hand

Joined: May 10, 2000
Posts: 1913
Alternatively, if it doesn't have to be Safari, you can use "open http://....." to open the URL in the default browser.

On a different tack, I've had problems with exec() before if I didn't at least start reading the input stream from the process. Somehow it got blocked. If it is a problem with exec(), let us know and we'll move this thread somewhere you'll get more help.
[ July 29, 2005: Message edited by: Simon Brown ]
Rick Beaver
Ranch Hand

Joined: Dec 14, 2004
Posts: 464
You probably need to invoke the shell before your command.

The open command will run from the shell - you need to use the shell to run it from java also.

Try adding /bin/sh to the start of your array and re-running it.

ph34r my 133t j4v4 h4><0r1ng sk177z
Pauline McNamara

Joined: Jan 19, 2001
Posts: 4012
I was pretty sure that from the command line you don't have to go into the application bundle to open Safari, so I played with this a little...

I got Safari to open to a blank page with this:

Runtime.getRuntime().exec( new String[] { "open" , "-a", "Safari" }) ;

Then, to try to open a specific url I tried

Runtime.getRuntime().exec( new String[] { "open" , "-a", "Safari", "" }) ;

This opened up to yahoo, but in Firefox, my default browser. After setting the preferences in Safari to make Safari the default browser, the above code worked fine.

Note that your code is giving "-a Safari" as one array element. That didn't work for me - separating them out did.

Maybe that helps?
[ July 29, 2005: Message edited by: Pauline McNamara ]
Ravi Sathish
Ranch Hand

Joined: Feb 26, 2002
Posts: 131
Thanks Guys,

Pauline your suggestion did work like a charm.

Does anybody happen to know how to get the name of default browser from the shell?

I agree. Here's the link:
subject: launching Safari from a Java App
It's not a secret anymore!