Opening a URL in the user's browser

My first thread in newless clubies :slight_smile:

So what is the best way to load up a browser when a user clicks a link?

I have some HTML help documentation that I would like loaded up in a browser. One option I guess is to use a Java HTML renderer but I think a full browser is better suited.

For windows the best way of loading a URL I can see is “start http://google.com/”. This will load the URL in the user’s default browser.

For other OS’s it gets a bit harder. I don’t like the idea of making an assumption for linux that a user has one browser or another, for example old code used to assume “netscape” but now many people don’t use that and use mozilla instead.

So I guess my choice is test for the existance of a particular browser (this could be changed in a user config file) and if none is found just display a box saing “goto http://google.com” - or just display the box first up.

There exists a Sourceforge project that has the right ideas http://browserlauncher.sourceforge.net/ but is too old now. Which presents a problem in itself - I don’t want to update this code every year so somthing which lasts is better. So is this windows “start” command going to be around for a while?

This article I also found but was of little help:
http://www.javaworld.com/javaworld/javatips/jw-javatip66.html
as i can’t see “rundll32 url.dll,FileProtocolHandler http://www.javaworld.com” being any better than a simple “start” command (I wounder if the author even knew about that command?).

I believe that “start” should run in windows 98 in the manner I am using it (file associations) as this document suggests: http://www.lagmonster.org/docs/DOS7/y-start.html

Does anyone have any experiances with this that they can share? Is there any magical way around this problem? Will Java1.5 have some spiffy method like “System.openURLInBroswer” 8)?

The best I have thus far is to use “start” with windows, and in linux test for the existance of “mozilla”, if it exists then use it else just show a text box. Enabling the user to change the linux/otherOS binary from “mozilla” to somthing else could help although it’s still not ideal.

Thanks in advance,

Will.

I just used that old browserlauncher code. It’ll have to do for now, but yes, it really needs an API call.

Cas :slight_smile:

You may try checking the $BROWSER environment variable and (after that) the /usr/bin/x-www-browser symlink. It’s not perfect.

And if all else fails (and it almost certainly will in Linux), why not resort to Swing’s HTML interface? That’s better than a “sorry can’t do it” message.

http://browserlauncher.sourceforge.net

[quote]And if all else fails (and it almost certainly will in Linux), why not resort to Swing’s HTML interface? That’s better than a “sorry can’t do it” message.
[/quote]
I guess so - for help files, but what if like in Alien Flux’s case you are trying to sell them somthing etc?

Thanks,

Will.

[quote]You may try checking the $BROWSER environment variable and (after that) the /usr/bin/x-www-browser symlink. It’s not perfect.
[/quote]
neither of those is setup on my system :frowning: - pity if it was a standard it would be great…

cas, so browserlauncher will work in all versions of Windows will it?

My only question is why they would use “rundll32 url.dll,FileProtocolHandler” and not just “start”?

They really do need to keep that project up to date - for example I’d say “mozilla” is a better guess on linux than “netscape” now. Is the mac code still current even?

This really should belong in the JVM - platform specific java code like this is ugly and not what Java should be.

Thanks all for your help,

Will.

I don’t see how it could possibly be in the JVM. For it to be in the JVM it’d have to offer at least decent success rate, preferably be 100% reliable. On Unix, especially Linux, there’s just no way. Unless Unix JVM’s prompted for a browser upon installation, even that has problems. Probably the one browser to be installed on more Linux boxes than anything is Lynx, woo.

well the JVM could search for common browsers such as netscape, mozilla in a “best effort” way, and could always return false if it failed. The good reasons for having it in the JVM is that the developer wouldn’t need to keep track of all the different operating systems that Java runs on nor keep it updated. Apple would be able to code a specific Mac one for example, and if Mac OS XI comes out with a different browser config it wouldn’t matter because the JVM for that OS would be updated.

It couldn’t be perfect, I agree but it would certainly be better.

Will.

A browserlauncher has to take care of a lot of things. Checking the $BROWSER variable first may always be a good idea (it’s not set up on many systems, but it gives the user a chance to specify the default browser). I don’t know if there are other ways to find out a user’s favourite browser (maybe you have to check first if the user runs KDE, Gnome or whatever). After you have done this you can try looking for global preferences, but this is dangerous, because the user’s personal preferences may be different than root’s preferences. Debian uses /etc/alternatives for this, but other distributions probably don’t have a concept for this.

Another way is to check the mailcap files. First you read in $HOME/.mailcap and $HOME/.mailcap.order. If these files don’t exist or don’t contain handlers for text/html you read /etc/.mailcap and /etc/.mailcap.order. You can start whichever browser comes first in /etc/.mailcap or is specified in /etc/.mailcap.order. /etc/.mailcap should contain most of the browser’s on the user’s system, so you may read them all in and let the user of your app choose which one to use.

Another thing you have to take care for is if you really want a text based browser. If you know that the pages you want to show can’t be displayed in a text based browser very well, you may choose a graphical browser, although a text based browser is the default.

If SWT is an option, they have a Program class which knows how to start a program associated as a viewer for a given document. This is, you’d simply do a Program.launch("file.html"); and let the system worry about the details.

[quote]cas, so browserlauncher will work in all versions of Windows will it?

My only question is why they would use “rundll32 url.dll,FileProtocolHandler” and not just “start”?
[/quote]
I think you meant to address me :slight_smile:
‘start’ is an executable program on win95/98, but on NT it is built in to the shell so can’t be “exec’d” directly like that.

I agree about keeping it up to date - but hey it’s open source - if you have updates pass them on. (I agree re: Mozilla vs. Netscape… but Opera is also a significant choice for Linux)

Well both of you actually, Cas did mention that it was what he used and I figured he may have done a fair bit of testing (being a commercial app).

Thanks for clearing that up :slight_smile:

Yeah, call me lazy but I was hoping to have a pre-packaged solution (surely an up to date one must exist?). But it looks like taking the browserlauncher code will be a good start (and for sure I shall send them any improvements…)

Maintaining a platform-specific browser launch code was exactally what I was trying to avoid when I asked this question - I don’t want to have to update these particular programs every year or so when the world’s browsers change :frowning: That is obviously a dream but failing that simply updaing my package with the updated jar of a well maintained browser launch package would also be nice :slight_smile:

Will.

[quote]If SWT is an option, they have a Program class which knows how to start a program associated as a viewer for a given document. This is, you’d simply do a Program.launch("file.html"); and let the system worry about the details.
[/quote]
SWT is not an option but it looks like they have thought about this issue very well :slight_smile:

Thanks,

Will.

[quote]A browserlauncher has to take care of a lot of things. Checking the $BROWSER variable first may always be a good idea (it’s not set up on many systems, but it gives the user a chance to specify the default browser). I don’t know if there are other ways to find out a user’s favourite browser (maybe you have to check first if the user runs KDE, Gnome or whatever). After you have done this you can try looking for global preferences, but this is dangerous, because the user’s personal preferences may be different than root’s preferences. Debian uses /etc/alternatives for this, but other distributions probably don’t have a concept for this.

Another way is to check the mailcap files. First you read in $HOME/.mailcap and $HOME/.mailcap.order. If these files don’t exist or don’t contain handlers for text/html you read /etc/.mailcap and /etc/.mailcap.order. You can start whichever browser comes first in /etc/.mailcap or is specified in /etc/.mailcap.order. /etc/.mailcap should contain most of the browser’s on the user’s system, so you may read them all in and let the user of your app choose which one to use.

Another thing you have to take care for is if you really want a text based browser. If you know that the pages you want to show can’t be displayed in a text based browser very well, you may choose a graphical browser, although a text based browser is the default.
[/quote]
linx being default could be a problem, however I’m guessing most people who have linx set as default have done it for a very good reason (it’s a great browser, I do love it’s simplicity but I don’t think I would ever make it my default). Unfortunatally you can’t even count on linx being present these days.

Thank you for the excellent tips - if I go down the road of modifying the browserlauncher code I shall definitally use them.

I was going to submit a bug about the mozilla/netscape thing but look at the bug list:
http://sourceforge.net/tracker/?atid=114506&group_id=14506&func=browse

Will.

Uhh, you can’t count on anything being present these days, you never could with Linux. Even more “formalized” Unices don’t offer many promises.

DistroWatch alone counts 100 Linux distros, most distros have multiple versions, there’s at least 10 browsers available for Linux, and Linux users can and do change their system as they please. There’s no central authority over Linux, it’s literally a very loose collection of programs sitting on top of a common kernel. At best this is a very difficult problem to solve for Linux, and at worst it’s impossible.

Well I think you could design a JVM which would have a pretty good stab at finding one graphical browser on the system and I would not say it’s impossible at all. Not that it matters since it probably will not happen anyway.

Sounds like the the browser launcher code is the best we have.

Let me ask - if I manage to get in contact with the developer of it, is anyone else here interested in perhaps joining the sourceforge project and updating it a bit (or failing that since, it’s been five years since it was updated and it’s open source we can always branch off a new project).

Will.