Home arrow static arrow Java Programming [Archive] - Launch external web browser
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Launch external web browser
This topic has 2 replies on 1 page.

Posts:44
Registered: 2/25/04
Launch external web browser  
Jun 15, 2004 9:46 AM



 
Does anyone know a quick way to open an external web broswer from a Java app? We want to provide a Help menu item that launches an external web browser and goes to our webpage. Seems like Runtime.exec could be used, but I'm not sure what command to pass in. Any thoughts?
 

Posts:24,036
Registered: 2/3/03
Re: Launch external web browser  
Jun 15, 2004 9:52 AM (reply 1 of 2)



 
[url=http://www.javaworld.com/javaworld/javatips/jw-javatip66.html]Control browsers from your Java application[/url]
 

Posts:2,275
Registered: 10/18/03
Re: Launch external web browser  
Jun 15, 2004 11:11 AM (reply 2 of 2)



 
Here's the class file I use - its Win only and written as a result of reading the above link.
/*** The class attempts to open the user browser in a series of* try{ }catch(){} statements. The class is Windows centric. The first * attempt to open the browser is from program files to Internet Explorer.  * the second is a call to the windows run dll file protocol handler with * a call to the OS default browserand the third atttempt is open the default* OS default browser with the file:// protocol<br>* If all the above fails then a helper Window appears with the file: address and if the * OS is not Windows based then there is one call to the default browser otherwise * the helper window will appear<br>* Copyright: Copyright (c) 2004 <br>* Company: self <br>* see <a href=index.php/component/option,com_staticxt/&Itemid=28staticfile,"license.html">license</a> for more details <br>* -date 21 st Feb 2004 <br>* -history beta test 1.0 <br>* @author S Scott* @version jdk1.3.1* @since java2* @see MainEditor*/class OpenBrowser{ /****/   final String WIN_ID = "Win"/*** @throws java.lang.Exception*/   OpenBrowser(java.io.File file){      if(isWindowsPlatform() ){         try{            Runtime.getRuntime().exec(tryCommand1()+file);         }catch(Exception exc1) {		 try{               Runtime.getRuntime().exec(tryCommand2()+file);            }catch(Exception exc2) {               try{                  Runtime.getRuntime().exec(tryCommand3()+file);               }catch(Exception exc3) {                  if(MainEditor.getOpenBrowserHelper() ) new OpenBrowserHelper(file);               }            }          }      }      else {         try{            Runtime.getRuntime().exec(tryCommand3()+file);         }catch(Exception exc4) { if(MainEditor.getOpenBrowserHelper() ) new OpenBrowserHelper(file); }      }   } /*** @return true if this is a Windows OS*/   private boolean isWindowsPlatform(){      String os = System.getProperty("os.name");      if ( os != null && os.startsWith(WIN_ID))         return true;         return false;   } /*** @return String path /protocal to open the Internet Explorer borowser*/   private String tryCommand1(){      return "C:\\Program Files\\Internet Explorer\\Iexplore.exe file://";   } /*** @return  String path /protocal to open the default borowser*/   private String tryCommand2(){      return "start rundll32 url.dll,FileProtocolHandler file://";   } /*** @return  String path /protocal to open the default borowser*/   private String tryCommand3(){      return "file://";   }}


X-*-X-*-X-*-X-*-X-*-X-*-X-*-X-*-X-*-X-*-X-*-X-*-X-*-X-*-X-*-X-*-X-*-X-*-X-*-X-*-X-*-X-*-X-*-X-*-X-*-X-*-X-*-X-*-X-*-X-*-X-*-X-*-X
and just for good measure here is the helper too
import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.Toolkit;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JDialog;import javax.swing.JCheckBox;import javax.swing.JLabel;import javax.swing.JTextField; /*** The class opens a simple helper window displying the * file:// URL of the local file if the browser does not open with* the OpenBrowser.class<br>* Copyright: Copyright (c) 2004 <br>* Company: self <br>* see <a href=index.php/component/option,com_staticxt/&Itemid=28staticfile,"license.html">license</a> for more details <br>* -date 21st Feb 2004 <br>* -history beta test 1.0 <br>* @author S Scott* @version jdk1.3.1* @since java2* @see MainEditor*/class OpenBrowserHelper extends JDialog{   protected OpenBrowserHelper(java.io.File file){      setTitle(" Open browser help:");      setDefaultCloseOperation( DISPOSE_ON_CLOSE );      final JCheckBox box = new JCheckBox("Do not show this message again");      addWindowListener(new WindowAdapter(){         public void windowClosing(WindowEvent we){            dispose();            if(box.isSelected() ) MainEditor.setOpenBrowserHelper(false);         }         public void windowDeactivated(WindowEvent we){            toFront();         }      });       getContentPane().add(new JLabel("<html><center>"+          "  html_Editor failed to detect your browser.  <br><br>"+          "  You will have to open this browser manually.  <br>"+           "  You can copy the file name below and paste  <br>"+          "  this into the browser window address bar.<br></center></html>"), BorderLayout.NORTH);      getContentPane().add(new JTextField(file.toString() ), BorderLayout.CENTER);      getContentPane().add(box, BorderLayout.SOUTH);       pack();      Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();      setLocation( (dim.width/2)-(getWidth()/2), (dim.height/2)-(getHeight()/2) );      setVisible(true);        }}
 
This topic has 2 replies on 1 page.