Java application to launch web browsers.

Hi,I'm writing an application and would like to be able to present a URL and as the user click on it, it will fire up the default web browser. I'm not doing it from an applet. If that can be done please let me know how. Thanks in advance!Dave
[285 byte] By [yangsx_22] at [2007-9-26 6:32:37]
# 1

HI Dave,

To launch an application you can use Runtime class.

Following is the code;

public void launchBrowser(String url) throws Exception

{

Runtime rt = Runtime.getRuntime();

try {

// for IE to launch;

Process proc = rt.exec("iexplore " + url);

}catch(Exception ex) {

// for netscape

Process proc = rt.exec("netscape " + url);

}

rt = null;

Bye,

S.M.Reddy.

smreddy_2000 at 2007-7-1 15:43:18 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2
Java Tip 66: Control browsers from your Java application http://www.javaworld.com/javaworld/javatips/jw-javatip66.html
jsalonen at 2007-7-1 15:43:18 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 3

Thanks for all the replies!

S.M.Reddy. I tried your code, everything compiled fine, but as I try to execute it nothing happened. Am I missing something?here's the code:

try

{

launchBrowser("http://www.jmu.edu");

}

catch (Exception e){}

public void launchBrowser(String url) throws Exception

{

Runtime rt = Runtime.getRuntime();

try {

// for IE to launch;

Process proc = rt.exec("iexplore " + url);

}catch(Exception ex) {

// for netscape

//Process proc = rt.exec("netscape " + url);

}

rt = null;

}

thanks again,

Dave

yangsx_22 at 2007-7-1 15:43:18 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 4

For one, you are missing exception handling code, so that no one has any clue what the problem might be.

Here's a probable cause: your system cannot find iexplore.exe or netscape.exe because their directories are not listed in the PATH environment variable. You'll need to either make sure that they are or use full paths - "c:/program files/netscape/netscape.exe" for example.

Or go the other way and let the system deside which browser to use: try to exec "start http://www.google.com" and "rundll32 url.dll,FileProtocolHandler http://www.google.com". This will work on windoze only, though...

jsalonen at 2007-7-1 15:43:18 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 5

Thanks again!

I was able to get the code from javaworld to work. One more question if you're still around. What swing text component would handle html tag or a link.Say I want to display a lot of text ( i use jtextarea), and there's a URL in the text, how would I be able to just click the URL and go to the website (what text component would handle links)?

thanks,

Dave

yangsx_22 at 2007-7-1 15:43:18 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 6
JTextPane; there's an example of using HyperLinkListeners in the API documentation.... somewhere (JTextPane? JEditorPane?)
jsalonen at 2007-7-1 15:43:18 > top of Java-index,Archived Forums,New To Java Technology Archive...