• Post Reply Bookmark Topic Watch Topic
  • New Topic

invoking a browser from application

 
selvas kumars
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I want to invoke a browser from my java application.Is it possible if so how??
My requirement is i want to invoke some html help docs from my application window...Please help me out..
regards,
silva.
 
Buckley Lee
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:
(Hopefully it's readable. I copied and pasted it to this window but the format got messed up so I edited it by hand here. After I press the 'Submit Reply' button I'm going to keep my fingers crossed in the hopes that it's still easy to read.)
When run, my browser is launched with the passed in file (local html file or internet site) being displayed.
/* =================================================================
This class launches the appropriate browser for the operating system the program is running on. Currently, this file only supports two operating systems: Windows NT/2000 and SunOS.
On Windows, rundll32.exe in the WINNT/system32 directory is used to launch the browser.
=================================================================
*/

// include necessary Java classes
import java.io.IOException;
import java.util.*;
import java.io.File;


public class StartBrowser
{
private static final String WIN_PATH = "rundll32";
private static final String
WIN_FLAG = "url.dll,FileProtocolHandler";


// constructor
//
// input is a String containing the url to display.
//
public StartBrowser(String urltext)
{
// first, determine what operating system the client is using
String osName = System.getProperty("os.name");

// Windows?
if((osName != null) && (osName.startsWith("Windows")))
{
// launch the Windows web browser
System.out.println("Windows environment");
LaunchWindowsBrowser(urltext);
}
else
if((osName != null) && (osName.compareTo("SunOS") == 0))
{
// launch the SunOS web browser
System.out.println("SunOS environment");
LaunchSunOSBrowser(urltext);
}
else
{
System.out.println("ERROR!! Unsupported operating" +
"system!!");
}
}


// function to start the SunOS browser
// NOTE: a script file is used to launch the browser. I didn't
// include the script file here. Refer to a Unix book to
// learn how to write your own script file to find/launch
// the browser. This code snippet shows you how to
// execute that script file.
private void LaunchSunOSBrowser(String urltext)
{
String commandString = null;

commandString = "<your script file>" + urltext;

System.out.println("commandString = '" + commandString
+ "'");

try {
// launch the browser
Process proc = Runtime.getRuntime().exec(commandString);
}
catch(java.io.IOException err)
{
System.err.println("Could not invoke the browser, command
= " + commandString);
System.err.println("Caught: " + err);
}
}


// function to start the Windows browser
private void LaunchWindowsBrowser(String urltext)
{
String commandString = null;

try {
// Command string used is:
// 'rundll32 url.dll,FileProtocolHandler http://...'
commandString = WIN_PATH + " " + WIN_FLAG + " " +
File.separator + urltext;

// launch the Windows browser
Process proc = Runtime.getRuntime().exec(commandString);
}
catch(java.io.IOException err)
{
System.err.println("Could not invoke the browser, command
= " + commandString);
System.err.println("Caught: " + err);
}
}

// TEST FUNCTION ONLY
public static void main(String[] args)
{
StartBrowser browser =
new StartBrowser("http://www.sun.com");
}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!