Open link in browser using java button?

How can I open a link in the default browser with a button click on the lines

button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { open("www.google.com"); // just what is the 'open' method? } }); 

?

+95
java browser swing hyperlink desktop
Jun 10 2018-12-12T00:
source share
8 answers

Use the Desktop # browse (URI) method. It opens the URI in the user's default browser.

 public static boolean openWebpage(URI uri) { Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null; if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) { try { desktop.browse(uri); return true; } catch (Exception e) { e.printStackTrace(); } } return false; } public static boolean openWebpage(URL url) { try { return openWebpage(url.toURI()); } catch (URISyntaxException e) { e.printStackTrace(); } return false; } 
+214
Jun 10 2018-12-12T00:
source share
 public static void openWebpage(String urlString) { try { Desktop.getDesktop().browse(new URL(urlString).toURI()); } catch (Exception e) { e.printStackTrace(); } } 
+34
Jun 02 '13 at 2:32
source share
 try { Desktop.getDesktop().browse(new URL("http://www.google.com").toURI()); } catch (Exception e) {} 

Note: you must enable the necessary imports from java.net

+24
Aug 19 '13 at 17:37
source share

Solution without a working environment BrowserLauncher2 . This solution is more general, as in Linux, Desktop is not always available.

The longest response sent to https://stackoverflow.com/a/312960/

+5
Jun 21 '15 at 12:07
source share
 private void ButtonOpenWebActionPerformed(java.awt.event.ActionEvent evt) { try { String url = "https://www.google.com"; java.awt.Desktop.getDesktop().browse(java.net.URI.create(url)); } catch (java.io.IOException e) { System.out.println(e.getMessage()); } } 
+2
May 20 '17 at 6:28
source share
 public static void openWebPage(String url) { try { Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null; if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) { desktop.browse(new URI(url)); } throw new NullPointerException(); } catch (Exception e) { JOptionPane.showMessageDialog(null, url, "", JOptionPane.PLAIN_MESSAGE); } } 
0
Nov 21 '17 at 12:42 on
source share

I know this is an old question, but sometimes Desktop.getDesktop() throws an unexpected failure, as in Ubuntu 18.04. Therefore, I have to rewrite my code as follows:

 public static void openURL(String domain) { String url = "https://" + domain; Runtime rt = Runtime.getRuntime(); try { if (MUtils.isWindows()) { rt.exec("rundll32 url.dll,FileProtocolHandler " + url).waitFor(); Debug.log("Browser: " + url); } else if (MUtils.isMac()) { String[] cmd = {"open", url}; rt.exec(cmd).waitFor(); Debug.log("Browser: " + url); } else if (MUtils.isUnix()) { String[] cmd = {"xdg-open", url}; rt.exec(cmd).waitFor(); Debug.log("Browser: " + url); } else { try { throw new IllegalStateException(); } catch (IllegalStateException e1) { MUtils.alertMessage(Lang.get("desktop.not.supported"), MainPn.getMainPn()); e1.printStackTrace(); } } } catch (IOException | InterruptedException e) { e.printStackTrace(); } } public static boolean isWindows() { return OS.contains("win"); } public static boolean isMac() { return OS.contains("mac"); } public static boolean isUnix() { return OS.contains("nix") || OS.contains("nux") || OS.indexOf("aix") > 0; } 

Then we can call this helper from the instance:

 button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { MUtils.openURL("www.google.com"); // just what is the 'open' method? } }); 
0
Aug 09 '18 at 4:31
source share

I do not know, but I am doing code in Java! To prove, see below.

  public class variables { public static void main(String[] args) { System.out.println("This is printing a variable bellow!"); string var = "This string is made of a variable!"; System.out.println(var); } } 

Did it prove it to you? If it is not, I am making an application!

0
May 18 '19 at 2:56
source share



All Articles