Go Back   Java Programming Forums > Learning Java > Java Code Snippets and Tutorials


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 30-05-2008, 09:54 AM
Senior Member
 

Join Date: May 2008
Posts: 19
Thanks: 0
Thanked 2 Times in 1 Post
Flash is on a distinguished road

I'm feeling Sneaky
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..

Java Code:
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);



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 06-05-2009, 04:16 PM
Junior Member
 

Join Date: May 2009
Posts: 24
Thanks: 1
Thanked 1 Time in 1 Post
Koâk is on a distinguished road
Default Re: How to Navigate to a URL with Runtime.getRuntime().exec()

Thanks for this, I always used this

Java Code:
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
Reply With Quote
  #3 (permalink)  
Old 25-06-2009, 07:41 PM
helloworld922's Avatar
Super Moderator
 

Join Date: Jun 2009
Posts: 1,329
Thanks: 5
Thanked 282 Times in 254 Posts
helloworld922 will become famous soon enoughhelloworld922 will become famous soon enoughhelloworld922 will become famous soon enough
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.

Java Code:
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());
         }
      }

   }
Reply With Quote
The Following User Says Thank You to helloworld922 For This Useful Post:
Koâk (27-06-2009)
  #4 (permalink)  
Old 27-06-2009, 11:10 AM
Junior Member
 

Join Date: May 2009
Posts: 24
Thanks: 1
Thanked 1 Time in 1 Post
Koâk is on a distinguished road
Default Re: How to Navigate to a URL with Runtime.getRuntime().exec()

Thank you very much for that
Reply With Quote
  #5 (permalink)  
Old 06-10-2009, 12:18 AM
Junior Member
 

Join Date: Oct 2009
Posts: 3
Thanks: 0
Thanked 1 Time in 1 Post
Trey is on a distinguished road
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.
Java Code:
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:
Java Code:
if (Desktop.isDesktopSupported())
            Desktop.getDesktop().browse(new URI("www.google.com"));
Reply With Quote
Reply

Tags
browser, url

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
Null Pointer Exception runtime error RoadRunner Exceptions 1 26-04-2009 06:21 PM


100 most searched terms
Search Cloud
2d arraylist java actionlistener actionlistener in java actionlistener java addactionlistener addactionlistener in java addactionlistener java applications of oops could not create java virtual machine xp double format java double to int double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java java 2d arraylist java actionlistener java addactionlistener java convert list to map java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming help java sendkeys java two dimensional arraylist java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics two dimensional arraylist java writing ipod apps

All times are GMT. The time now is 08:56 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.