Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

launching Safari from a Java App

 
Ravi Sathish
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1913
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic