• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

launching Safari from a Java App

 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43024
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..."
 
sharp shooter, and author
Posts: 1913
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic