public class BrowserLauncher
extends java.lang.Object
Yes, this is platform-specific code, and yes, it may rely on classes on certain platforms that are not part of the standard JDK. What we're trying to do, though, is to take something that's frequently desirable but inherently platform-specific -- opening a default browser -- and allow programmers (you, for example) to do so without worrying about dropping into native code or doing anything else similarly evil.
Anyway, this code is completely in Java and will run on all JDK 1.1-compliant systems without modification or a need for additional libraries. All classes that are required on certain platforms to allow this to run are dynamically loaded at runtime via reflection and, if not found, will not cause this to do anything other than returning an error when opening the browser.
There are certain system requirements for this class, as it's running through Runtime.exec(), which is Java's way of making a native system call. Currently, this requires that a Macintosh have a Finder which supports the GURL event, which is true for Mac OS 8.0 and 8.1 systems that have the Internet Scripting AppleScript dictionary installed in the Scripting Additions folder in the Extensions folder (which is installed by default as far as I know under Mac OS 8.0 and 8.1), and for all Mac OS 8.5 and later systems. On Windows, it only runs under Win32 systems (Windows 95, 98, and NT 4.0, as well as later versions of all). On other systems, this drops back from the inherently platform-sensitive concept of a default browser and simply attempts to launch Firefox via a shell command.
This code is Copyright 1999-2001 by Eric Albert (ejalbert@cs.stanford.edu) and may be redistributed or modified in any form without restrictions as long as the portion of this comment from this paragraph through the end of the comment is not removed. The author requests that he be notified of any application, applet, or other binary that makes use of this code, but that is more out of curiosity than anything and is not required. This software includes no warranty. The author is not responsible for any loss of data or functionality or any adverse or unexpected effects of using this software.
Credits:
Steven Spencer, JavaWorld magazine (Java//Tip 66)
Thanks also to Ron B. Yeh, Eric Shapiro, Ben Engber, Paul Teitlebaum, Andrea Cantatore, Larry Barowski, Trevor Bedzek, Frank Miedrich, and Ron Rabakukk
On November 3, 2003, BrowserLauncher was downloaded from
http://browserlauncher.sourceforge.net/.
That URL no longer exists, there is a replacement package which is LGPL'd.
See http://web.archive.org/web/20031028032907/http://browserlauncher.sourceforge.net/
for the original web page.
Red (cxh) |
Red (cxh) |
Modifier and Type | Field and Description |
---|---|
static boolean |
delayExit
Set to true if we copied a file out of a jar file so that
the browser could display it.
|
Modifier and Type | Method and Description |
---|---|
static void |
main(java.lang.String[] args)
Launch the browser on the first argument.
|
static void |
openURL(java.lang.String url)
Attempts to open the default web browser to the given URL.
|
public static boolean delayExit
public static void main(java.lang.String[] args) throws java.lang.Exception
For example, to open the user's default browser on http://www.eecs.berkeley.edu
java -classpath $PTII ptolemy.actor.gui.BrowserLauncher http://www.eecs.berkeley.edu
args
- An array of command line arguments. The first
argument names a URL to be opened. If there is no first
argument, then open http://ptolemy.eecs.berkeley.edu. Second
and subsequent arguments are ignored.java.lang.Exception
- If there is a problem launching the browser.public static void openURL(java.lang.String url) throws java.io.IOException
We use the following strategy to find URLs that may be inside
jar files:
If the string does not start with "http": see if it is a
file.
If the file cannot be found, look it up in the classpath.
If the file can be found in the classpath then use the
found file instead of the given URL.
If the file cannot be found in the classpath, then pass the
original given URL to the browser.
If the ptolemy.ptII.browser property is set, then its value
is used as the value of the browser.
To always use Internet Explorer, one might invoke Ptolemy
with:
java -classpath $PTII -Dptolemy.ptII.browser=c:\\Program\ Files\\Internet\ Explorer\\iexplore.exe ptolemy.vergil.VergilApplication
To always use Firefox:
java -classpath $PTII -Dptolemy.ptII.browser=c:\\Program\ Files\\Mozilla\ Firefox\\firefox ptolemy.vergil.VergilApplication
To preserve your browser choice set the ptolemy.ptII.browser
property in $PTII/lib/ptII.properties
. Note that
each time $PTII/bin/configure
is run,
$PTII/lib/ptII.properties.in
is read and
$PTII/lib/ptII.properties
is overwritten, so you
may want to add your changes to
co * $PTII/lib/ptII.properties.in
.
url
- The URL to open.
It is best if the first argument is an absolute URL
as opposed to a relative URL.java.io.IOException
- If the web browser could not be located or
does not run