How To Start Internet Explorer from a Java Application (283225)



The information in this article applies to:

  • Microsoft SDK for Java 4.0
  • Microsoft Internet Explorer 5.0 for Windows 98
  • Microsoft Internet Explorer 5.01 for Windows 98 SP 1
  • Microsoft Internet Explorer 5.5 for Windows 98 SP 1

This article was previously published under Q283225

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.

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("");
   }
}
				

REFERENCES

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

Modification Type:MajorLast Reviewed:6/14/2006
Keywords:kbhowto kbJava KB283225 kbAudDeveloper