Stolen from this guy.
public void openURL(String url)
{
try
{
String osName = System.getProperty("os.name");
if (osName.startsWith("Mac OS"))
{
Class fileMgr =
Class.forName("com.apple.eio.FileManager");
Method openURL =
fileMgr.getDeclaredMethod("openURL",
new Class[]{String.class});
openURL.invoke(null, new Object[]{url});
}
else if (osName.startsWith("Windows"))
{
String windowsExec =
"rundll32 url.dll, FileProtocolHandler " + url;
Runtime.getRuntime().exec(windowsExec);
}
else
{
// assume Unix or Linux
String[] browsers =
{
"firefox", "opera", "konqueror",
"epiphany", "mozilla", "netscape"
};
String browser = null;
for (int i = 0;
i < browsers.length && browser == null; i++)
{
String[] candidateBrowser =
new String[]{"which", browsers[i]};
Process exec =
Runtime.getRuntime().exec(candidateBrowser);
if (exec.waitFor() == 0)
{
browser = browsers[i];
}
}
if (browser != null)
{
Runtime.getRuntime().exec(new String[]
{browser, url});
}
else
{
String message =
"You sure you have web browser?";
throw new RuntimeException(message);
}
}
}
catch (Exception e)
{
// Ha, ha, ha.
// ClassNotFoundException, SecurityException,
// NoSuchMethodException, IOException
// InterruptedException, IllegalArgumentException,
// IllegalAccessException and InvocationTargetException
LOGGER.error(e);
throw new RuntimeException(e);
}
}
Note that the url has to be fully qualified, e.g., http://www.abbablows.blogspot.com.
No comments:
Post a Comment