Skip to content

Commit 06c3782

Browse files
committed
Upgrade BareBonesBrowserLauncher; improve linux support
Upgraded from 1.5 to latest version 3.2. Fixed deprecated error. Added xdg-open as first choice on linux systems. fixes #65 Signed-off-by: Tony Germano <[email protected]>
1 parent 47e75ee commit 06c3782

File tree

2 files changed

+123
-72
lines changed

2 files changed

+123
-72
lines changed
Lines changed: 62 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,73 @@
1-
// ///////////////////////////////////////////////////////
2-
// Bare Bones Browser Launch //
3-
// Version 1.5 //
4-
// December 10, 2005 //
5-
// Supports: Mac OS X, GNU/Linux, Unix, Windows XP //
6-
// Example Usage: //
7-
// String url = "http://www.centerkey.com/"; //
8-
// BareBonesBrowserLaunch.openURL(url); //
9-
// Public Domain Software -- Free to Use as You Like //
10-
// ///////////////////////////////////////////////////////
1+
// SPDX-License-Identifier: MPL-2.0
2+
// SPDX-FileCopyrightText: 2025 Tony Germano <[email protected]>
3+
// SPDX-Contributor: Original Author Dem Pilafian (Original work licensed under WTFPL)
4+
115
package com.mirth.connect.client.ui;
126

13-
import java.lang.reflect.Method;
7+
import java.util.Arrays;
148

9+
/**
10+
* Utility class to open a web page from a Swing application
11+
* in the user's default browser.
12+
* <p>
13+
* Supports: Mac OS X, Linux, Unix, Windows
14+
* <p>
15+
* Example usage:<br>
16+
* <code>&nbsp;&nbsp;
17+
* String url = "https://dna-engine.org/";<br>&nbsp;&nbsp;
18+
* BareBonesBrowserLaunch.openURL(url);</code>
19+
* <p>
20+
* Latest Version: <a href=https://centerkey.com/java/browser>
21+
* https://centerkey.com/java/browser</a>
22+
* <p>
23+
* Published: October 24, 2010
24+
* Modified: 2025
25+
* <p>
26+
*
27+
* @author Dem Pilafian
28+
* @version 3.2
29+
*/
1530
public class BareBonesBrowserLaunch {
1631

32+
static final String[] browsers = { "xdg-open", "x-www-browser", "google-chrome",
33+
"firefox", "opera", "epiphany", "konqueror", "conkeror", "midori",
34+
"kazehakase", "mozilla" };
35+
36+
/**
37+
* Open the specified web page in the user's default browser
38+
*
39+
* @param url A web address (URL) of a web page (example:
40+
* <code>"https://dna-engine.org/"</code>)
41+
*/
1742
public static void openURL(String url) {
18-
String osName = System.getProperty("os.name");
19-
try {
20-
if (osName.startsWith("Mac OS")) {
21-
Class<?> fileMgr = Class.forName("com.apple.eio.FileManager");
22-
Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class });
23-
openURL.invoke(null, new Object[] { url });
24-
} else if (osName.startsWith("Windows")) {
25-
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
26-
} else {
27-
// assume Unix or Linux
28-
String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla",
29-
"netscape" };
30-
String browser = null;
31-
for (int count = 0; count < browsers.length && browser == null; count++) {
32-
if (Runtime.getRuntime().exec(new String[] { "which",
33-
browsers[count] }).waitFor() == 0) {
34-
browser = browsers[count];
35-
}
36-
}
37-
if (browser == null) {
38-
throw new Exception("Could not find web browser");
39-
} else {
40-
Runtime.getRuntime().exec(new String[] { browser, url });
43+
try { // attempt to use Desktop library from JDK 1.6+
44+
Class<?> d = Class.forName("java.awt.Desktop");
45+
d.getDeclaredMethod("browse",
46+
new Class<?>[] { java.net.URI.class }).invoke(
47+
d.getDeclaredMethod("getDesktop").invoke(null),
48+
new Object[] { java.net.URI.create(url) });
49+
} catch (Exception ignore) { // library not available or failed
50+
String osName = System.getProperty("os.name");
51+
try {
52+
if (osName.startsWith("Mac OS")) {
53+
Class.forName("com.apple.eio.FileManager").getDeclaredMethod(
54+
"openURL", new Class<?>[] { String.class }).invoke(null,
55+
new Object[] { url });
56+
} else if (osName.startsWith("Windows"))
57+
Runtime.getRuntime().exec(new String[] {
58+
"rundll32", "url.dll,FileProtocolHandler", url });
59+
else { // assume Unix or Linux
60+
String browser = null;
61+
for (String b : browsers)
62+
if (browser == null
63+
&& Runtime.getRuntime().exec(new String[] { "which", b }).getInputStream().read() != -1)
64+
Runtime.getRuntime().exec(new String[] { browser = b, url });
65+
if (browser == null)
66+
throw new Exception(Arrays.toString(browsers));
4167
}
68+
} catch (Exception e) {
69+
PlatformUI.MIRTH_FRAME.alertThrowable(PlatformUI.MIRTH_FRAME, e);
4270
}
43-
} catch (Exception e) {
44-
PlatformUI.MIRTH_FRAME.alertThrowable(PlatformUI.MIRTH_FRAME, e);
4571
}
4672
}
4773
}
Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,73 @@
1-
// Bare Bones Browser Launch //
2-
// Version 1.5 //
3-
// December 10, 2005 //
4-
// Supports: Mac OS X, GNU/Linux, Unix, Windows XP //
5-
// Example Usage: //
6-
// String url = "http://www.centerkey.com/"; //
7-
// BareBonesBrowserLaunch.openURL(url); //
8-
// Public Domain Software -- Free to Use as You Like //
9-
/////////////////////////////////////////////////////////
1+
// SPDX-License-Identifier: MPL-2.0
2+
// SPDX-FileCopyrightText: 2025 Tony Germano <[email protected]>
3+
// SPDX-Contributor: Original Author Dem Pilafian (Original work licensed under WTFPL)
4+
105
package com.mirth.connect.manager;
116

12-
import java.lang.reflect.Method;
7+
import java.util.Arrays;
138

9+
/**
10+
* Utility class to open a web page from a Swing application
11+
* in the user's default browser.
12+
* <p>
13+
* Supports: Mac OS X, Linux, Unix, Windows
14+
* <p>
15+
* Example usage:<br>
16+
* <code>&nbsp;&nbsp;
17+
* String url = "https://dna-engine.org/";<br>&nbsp;&nbsp;
18+
* BareBonesBrowserLaunch.openURL(url);</code>
19+
* <p>
20+
* Latest Version: <a href=https://centerkey.com/java/browser>
21+
* https://centerkey.com/java/browser</a>
22+
* <p>
23+
* Published: October 24, 2010
24+
* Modified: 2025
25+
* <p>
26+
*
27+
* @author Dem Pilafian
28+
* @version 3.2
29+
*/
1430
public class BareBonesBrowserLaunch {
1531

16-
private static final String errMsg = "Error attempting to launch web browser";
32+
static final String[] browsers = { "xdg-open", "x-www-browser", "google-chrome",
33+
"firefox", "opera", "epiphany", "konqueror", "conkeror", "midori",
34+
"kazehakase", "mozilla" };
1735

36+
/**
37+
* Open the specified web page in the user's default browser
38+
*
39+
* @param url A web address (URL) of a web page (example:
40+
* <code>"https://dna-engine.org/"</code>)
41+
*/
1842
public static void openURL(String url) {
19-
String osName = System.getProperty("os.name");
20-
try {
21-
if (osName.startsWith("Mac OS")) {
22-
Class fileMgr = Class.forName("com.apple.eio.FileManager");
23-
Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class });
24-
openURL.invoke(null, new Object[] { url });
25-
} else if (osName.startsWith("Windows")) {
26-
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
27-
} else {
28-
// assume Unix or Linux
29-
String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla",
30-
"netscape" };
31-
String browser = null;
32-
for (int count = 0; count < browsers.length && browser == null; count++) {
33-
if (Runtime.getRuntime().exec(new String[] { "which",
34-
browsers[count] }).waitFor() == 0) {
35-
browser = browsers[count];
36-
}
37-
}
38-
if (browser == null) {
39-
throw new Exception("Could not find web browser");
40-
} else {
41-
Runtime.getRuntime().exec(new String[] { browser, url });
43+
try { // attempt to use Desktop library from JDK 1.6+
44+
Class<?> d = Class.forName("java.awt.Desktop");
45+
d.getDeclaredMethod("browse",
46+
new Class<?>[] { java.net.URI.class }).invoke(
47+
d.getDeclaredMethod("getDesktop").invoke(null),
48+
new Object[] { java.net.URI.create(url) });
49+
} catch (Exception ignore) { // library not available or failed
50+
String osName = System.getProperty("os.name");
51+
try {
52+
if (osName.startsWith("Mac OS")) {
53+
Class.forName("com.apple.eio.FileManager").getDeclaredMethod(
54+
"openURL", new Class<?>[] { String.class }).invoke(null,
55+
new Object[] { url });
56+
} else if (osName.startsWith("Windows"))
57+
Runtime.getRuntime().exec(new String[] {
58+
"rundll32", "url.dll,FileProtocolHandler", url });
59+
else { // assume Unix or Linux
60+
String browser = null;
61+
for (String b : browsers)
62+
if (browser == null
63+
&& Runtime.getRuntime().exec(new String[] { "which", b }).getInputStream().read() != -1)
64+
Runtime.getRuntime().exec(new String[] { browser = b, url });
65+
if (browser == null)
66+
throw new Exception(Arrays.toString(browsers));
4267
}
68+
} catch (Exception e) {
69+
ManagerController.getInstance().alertErrorDialog("Could not open web browser.");
4370
}
44-
} catch (Exception e) {
45-
ManagerController.getInstance().alertErrorDialog("Could not open web browser.");
4671
}
4772
}
4873
}

0 commit comments

Comments
 (0)