aspose file tools*
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
JavaRanch » Java Forums » Engineering » Mac OS
Bookmark "launching Safari from a Java App" Watch "launching Safari from a Java App" New topic
Author

launching Safari from a Java App

Ravi Sathish
Ranch Hand

Joined: Feb 26, 2002
Posts: 131
Ranchers,

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/Safari.app/Contents/MacOS/ http://www.yahoo.com

I would appreciate any solutions,suggestions, and workarounds

TIA
Ravi
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42599
    
  65
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..."


Ping & DNS - my free Android networking tools app
Simon Brown
sharp shooter, and author
Ranch Hand

Joined: May 10, 2000
Posts: 1913
    
    6
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
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
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", "http://www.yahoo.com" }) ;

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 http://www.yahoo.com" as one array element. That didn't work for me - separating them out did.

Maybe that helps?
Pauline
[ 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?

Ravi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: launching Safari from a Java App