Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to Navigate to a URL with Runtime.getRuntime().exec()

  1. #1
    Senior Member
    Join Date
    May 2008
    Posts
    19
    Thanks
    0
    Thanked 9 Times in 4 Posts

    Post How to Navigate to a URL with Runtime.getRuntime().exec()

    With this code you can open a web browser and navigate to a given URL.

    You can place this in the Main method or in a JButton etc..

    String[] cmd = new String[4];
    cmd[0] = "cmd.exe";
    cmd[1] = "/C";
    cmd[2] = "start";
    cmd[3] = "http://www.javaprogrammingforums.com";
    Process p = Runtime.getRuntime().exec(cmd);


  2. #2
    Junior Member
    Join Date
    May 2009
    Posts
    25
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: How to Navigate to a URL with Runtime.getRuntime().exec()

    Thanks for this, I always used this

        public static void openBrowser(String URL) {
                String browserPath = "C:/Program Files/Mozilla Firefox/firefox.exe";
                String url = URL;
                	try {
                     		String[] b = {browserPath, url};
                     		Runtime.getRuntime().exec(b);
                		 }
                 		catch (Exception exc) {
              		exc.printStackTrace();
                 		}
    	}
    but it didn't work for people without Mozilla Firefox

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to Navigate to a URL with Runtime.getRuntime().exec()

    Link.

    Cause the page was hard to read, here's the code he had.

    package com.centerkey.utils;
     
    import java.lang.reflect.Method;
    import javax.swing.JOptionPane;
    import java.util.Arrays;
     
    /**
     * <b>Bare Bones Browser Launch for Java</b><br>
     * Utility class to open a web page from a Swing application
     * in the user's default browser.<br>
     * Supports: Mac OS X, GNU/Linux, Unix, Windows XP/Vista<br>
     * Example Usage:<code><br> &nbsp; &nbsp;
     *    String url = "http://www.google.com/";<br> &nbsp; &nbsp;
     *    BareBonesBrowserLaunch.openURL(url);<br></code>
     * Latest Version: <a href="http://www.centerkey.com/java/browser/">www.centerkey.com/java/browser</a><br>
     * Author: Dem Pilafian<br>
     * Public Domain Software -- Free to Use as You Like
     * @version 2.0, May 26, 2009
     */
    public class BareBonesBrowserLaunch {
     
       static final String[] browsers = { "firefox", "opera", "konqueror", "epiphany",
          "seamonkey", "galeon", "kazehakase", "mozilla", "netscape" };
     
       /**
        * Opens the specified web page in a web browser
        * @param url A web address (URL) of a web page (ex: "http://www.google.com/")
        */
       public static void openURL(String url) {
          String osName = System.getProperty("os.name");
          try {
             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"))
                Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
             else { //assume Unix or Linux
                boolean found = false;
                for (String browser : browsers)
                   if (!found) {
                      found = Runtime.getRuntime().exec(
                         new String[] {"which", browser}).waitFor() == 0;
                      if (found)
                         Runtime.getRuntime().exec(new String[] {browser, url});
                      }
                if (!found)
                   throw new Exception(Arrays.toString(browsers));
                }
             }
          catch (Exception e) {
             JOptionPane.showMessageDialog(null,
                "Error attempting to launch web browser\n" + e.toString());
             }
          }
     
       }

  4. The Following User Says Thank You to helloworld922 For This Useful Post:

    Koâk (June 27th, 2009)

  5. #4
    Junior Member
    Join Date
    May 2009
    Posts
    25
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: How to Navigate to a URL with Runtime.getRuntime().exec()

    Thank you very much for that

  6. #5
    Junior Member
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to Navigate to a URL with Runtime.getRuntime().exec()

    Quote Originally Posted by helloworld922 View Post
    Link.

    Cause the page was hard to read, here's the code he had.
    package com.centerkey.utils;
     
    import java.lang.reflect.Method;
    import javax.swing.JOptionPane;
    import java.util.Arrays;
     
    /**
     * <b>Bare Bones Browser Launch for Java</b><br>
     * Utility class to open a web page from a Swing application
     * in the user's default browser.<br>
     * Supports: Mac OS X, GNU/Linux, Unix, Windows XP/Vista<br>
     * Example Usage:<code><br> &nbsp; &nbsp;
     *    String url = "http://www.google.com/";<br> &nbsp; &nbsp;
     *    BareBonesBrowserLaunch.openURL(url);<br></code>
     * Latest Version: <a href="http://www.centerkey.com/java/browser/">www.centerkey.com/java/browser</a><br>
     * Author: Dem Pilafian<br>
     * Public Domain Software -- Free to Use as You Like
     * @version 2.0, May 26, 2009
     */
    public class BareBonesBrowserLaunch {
     
       static final String[] browsers = { "firefox", "opera", "konqueror", "epiphany",
          "seamonkey", "galeon", "kazehakase", "mozilla", "netscape" };
     
       /**
        * Opens the specified web page in a web browser
        * @param url A web address (URL) of a web page (ex: "http://www.google.com/")
        */
       public static void openURL(String url) {
          String osName = System.getProperty("os.name");
          try {
             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"))
                Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
             else { //assume Unix or Linux
                boolean found = false;
                for (String browser : browsers)
                   if (!found) {
                      found = Runtime.getRuntime().exec(
                         new String[] {"which", browser}).waitFor() == 0;
                      if (found)
                         Runtime.getRuntime().exec(new String[] {browser, url});
                      }
                if (!found)
                   throw new Exception(Arrays.toString(browsers));
                }
             }
          catch (Exception e) {
             JOptionPane.showMessageDialog(null,
                "Error attempting to launch web browser\n" + e.toString());
             }
          }
     
       }

    I've always used:
    if (Desktop.isDesktopSupported())
                Desktop.getDesktop().browse(new URI("www.google.com"));

Similar Threads

  1. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM

Tags for this Thread