Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How To Start Internet Explorer from a Java Application


View products that this article applies to.

This article was previously published under Q283225

↑ Back to the top


Summary

It has been widely documented on the Internet that you should use the following code to start your default browser from a Java application:
     rundll32 url.dll,FileProtocolHandler http://www.microsoft.com/ms.htm
				
However, when Internet Explorer is identified as your default browser, you receive the following error message:
Unable to open "http://www.microsoft.com/ms.htm".
This code does work if you do not specify the page as follows:
     rundll32 url.dll,FileProtocolHandler http://www.microsoft.com/ 
				
NOTE: This solution is considered obsolete. It is now documented that you should use the ShellExecute function.

↑ Back to the top


More information

The following code samples demonstrate two possible solutions for the above-mentioned procedure.

Solution Using the ShellExecute function

The following code uses the ShellExecute function:
import com.ms.com.*;
import com.ms.win32.*;
import java.io.*;
import com.ms.lang.*;
import com.ms.wfc.html.om.shdocvw.*;

/**
 * @com.register ( clsid=A1902ECA-A3B2-4DD5-840B-648CE61D48E6, 
typelib=631CF9B2-7570-4119-8703-634B4F847897 )
 */ 
public class BrowserLaunch
{
   public static void main (String[] args)  
   {
      try
      {
         BrowserLaunch app = new BrowserLaunch();
         String defBrowser = app.checkDefaultBrowser();
         app.launchDefault("http://www.microsoft.com/ms.htm");
      }
      catch(Exception e)
      {
         System.out.println(e.getMessage());
      }
   }

   public String checkDefaultBrowser() throws Exception
   {
      //HKEY_CLASSES_ROOT\http\shell\open\ddeexec\Application
      RegKey rkey = new RegKey(RegKey.CLASSES_ROOT, 
http\\shell\\open\\ddeexec\\Application", RegKey.KEYOPEN_READ);

      return rkey.getStringValue("");
   }

   public void launchDefault(String file) throws Exception
   {
      int retval;
      retval = Shell32.ShellExecute(0,"open",file,null,null,wins.SW_SHOWNORMAL);

      if (retval <= 32)
         alert("Problem with URL.");
   }

   private void alert(String msg)
   {
      User32.MessageBox(0,msg,"Alert!",winm.MB_OK);
   }
}
				

Solution Using the Runtime Class

The following code uses the Runtime class:
import java.io.IOException;
import com.ms.com.*;
import com.ms.lang.*;

public class Class1
{
   /**
    * This is the main entry point for the application. 
    *
    * @param args Array of parameters are passed to the application
    * through the command line.
    */ 
   public static void main (String[] args) throws Exception
   {
      Class1 app = new Class1();
      String defBrowser = app.checkDefaultBrowser();
      if (defBrowser.equalsIgnoreCase("IExplore"))
      try
      {
         Runtime rt = Runtime.getRuntime(); 
         String  path = app.getBrowserExecutablePath();
         //Process a = rt.exec("c:\\Program Files\\Internet 
Explorer\\iexplore http://www.microsoft.com/ms.htm");
         Process a = rt.exec(path + "http://www.microsoft.com/ms.htm"); 
      }	
      catch(Exception e) 
      { 
         System.out.println(e); 
      } 
   }

   public String checkDefaultBrowser() throws Exception
   {
      //HKEY_CLASSES_ROOT\http\shell\open\ddeexec\Application
      RegKey rkey = new RegKey(RegKey.CLASSES_ROOT,
"http\\shell\\open\\ddeexec\\Application", RegKey.KEYOPEN_READ);
		
      return rkey.getStringValue("");
   }

   public String getBrowserExecutablePath() throws Exception
   {
      //HKEY_CLASSES_ROOT\http\shell\open\command\default
      RegKey rkey = new RegKey(RegKey.CLASSES_ROOT, 
"http\\shell\\open\\command",RegKey.KEYOPEN_READ);

      return rkey.getStringValue("");
   }
}
				

↑ Back to the top


References

For support information about Visual J++ and the SDK for Java, visit the following Microsoft Web site:

↑ Back to the top


Keywords: KB283225, kbjava, kbhowto

↑ Back to the top

Article Info
Article ID : 283225
Revision : 4
Created on : 6/14/2006
Published on : 6/14/2006
Exists online : False
Views : 492