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
  #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: 835
Thanks: 5
Thanked 187 Times in 172 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
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java actionlistener java actionlistener jbutton addactionlistener addactionlistener java avatar hardware id convert double to integer java double format java 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.outofmemoryerror: java heap space format double java get mouse position java hardware id avatar java 2 dimensional arraylist java 2d arraylist java actionlistener java addactionlistener java button actionlistener java convert double to int java double format java double to int java double to integer java for beginner eclippse java format double java forum java forums java get mouse position java jbutton java list to map java mouse position java programming forum java programming forums java sendkeys java.lang.reflect.invocationtargetexception java.util.arraylist jbutton actionlistener jbutton java jtextarea font size programming forums string to int java two dimensional arraylist java writing apps for ipod touch

All times are GMT. The time now is 05:33 PM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.