Java Code to Open a Web Page

Some times the simplest things drive you nuts. 🙂

The Desktop class (java.awt.Desktop) was introduced in Java v1.6. There are occasions when you are running a Java application in JVM 1.6 or higher that the Desktop class is not available or fails to load.

It is just weird and very frustrating. Its not just me. If you do an internet search, you will find lots of people with the same problem and weirdly, Ubuntu is an operating system that has this problem a lot.

I decided to create a simple “wrapper” class to open a web page (URL). First, it will attempt to load the Java Desktop class and if it fails or is not available then it will use the Runtime class to open the web page. My Java class should be able to run in any release of Java (JVM) and on pretty much any platform.

You can download the source code from here. Enjoy. 🙂

/**
 * A simple class to open a web page (URL) in the default web browser.
 * First, it will attempt to use the Desktop class that is available in
 * Java 1.6 or higher.  If it fails or is not available then it will attempt
 * to open the web page using the Runtime class.
 *
 * @author Roger Lacroix
 * @version 1.0.0
 * @license Apache 2 License
 */
public class WebPage
{
   public static void main(String[] args)
   {
        String url = "https://www.google.com";
        WebPage.open(url);
   }

   /**
    * Open a URL in the default web browser.
    * @param url
    * @return true/false
    */
   public static boolean open(String url)
   {
      boolean flag = false;

      try
      {
         /**
          *  Try to load the Desktop Class - only available in Java 1.6 or higher
          *  The code performs the following call:
          *  <code>java.awt.Desktop.getDesktop().browse(url)</code>
          */
         Class<?> cc = Class.forName("java.awt.Desktop");
         cc.getDeclaredMethod("browse", new Class[] { java.net.URI.class }).invoke(
               cc.getDeclaredMethod("getDesktop").invoke(null),
               new Object[] { java.net.URI.create(url) } );

         flag = true;
      }
      catch (Exception skip)
      {  // The Desktop Class either failed to load or is not available.
         String OS = System.getProperty("os.name").toLowerCase();
         Runtime rt = Runtime.getRuntime();
         try
         {
            if (OS.contains("win"))
            {
               rt.exec("rundll32 url.dll,FileProtocolHandler " + url).waitFor();
               flag = true;
            }
            else if (OS.contains("mac"))
            {
               String[] cmd = { "open", url };
               rt.exec(cmd).waitFor();
               flag = true;
            }
            else if (OS.contains("nix") || OS.contains("nux"))
            {
               String[] cmd = { "xdg-open", url };
               rt.exec(cmd).waitFor();
               flag = true;
            }
            else
            {
               System.out.println("Unknown operating system: "+ OS + " : cannot open web page.");
            }
         }
         catch (Exception e)
         {
            e.printStackTrace();
         }
      }

      return flag;
   }
}

Regards,
Roger Lacroix
Capitalware Inc.

This entry was posted in Java, Linux, macOS (Mac OS X), Open Source, Programming, Raspberry Pi, Unix, Windows.

Comments are closed.