wood burning stoves*
The moose likes Java in General and the fly likes Opening a Browser From Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Opening a Browser From Java" Watch "Opening a Browser From Java" New topic
Author

Opening a Browser From Java

Jithesh Kozhipurath
Ranch Hand

Joined: Aug 27, 2003
Posts: 76
hai friends,
I have a url string, i want to open it on web browser when clicked on a label. I know how to open the browser, but how can i open the url in that browser.
Thanks in advance,


Jithesh K
Mahesh swami
Greenhorn

Joined: Aug 13, 2002
Posts: 26
You can write
Runtime.exec("Iexplore http://www.yahoo.com");
Jeyachandran Perumal
Greenhorn

Joined: Jun 18, 2003
Posts: 10
if you want to open a Browser from an Applet
use
AppletContext ac = getAppletContext();
ac.showDocument(new URL("http://java.sun.com"),"_blank");

Hope it helps


Jeyachandran Perumal<br />-----------------------------<br />Sun cetified Java Programmer<br />Sun Certified Web Component Developer
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Using Runtime.exec() to launch a browser will work, although it will be platform dependent: Linux and Mac systems won't have Internet Explorer on them. One platform-independent option would be to create a JFrame with a JEditorPane in it, and display the URL you want in that JEditorPane. This works fine for straight HTML, anyway.


[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
My favorite Windows technique is to shell to rundll32. This is just like typing a URL as a DOS command - try it! - or clicking a file in Windows Explorer. It launches the default application for the file extension. So if your user prefers another browser, they get that. If you launch a DOC file they get MS-Word, etc.
Here's a JavaWorld tip that uses this technique for Windows and some other stuff I never bothered to read for Unix.
Another way to run IE is via Jacob or some other Java CCOM bridge. See DanAdler.com for more. This is a solidly Windows-only approach.
[ September 03, 2003: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jithesh Kozhipurath
Ranch Hand

Joined: Aug 27, 2003
Posts: 76
Thank you friends,
But i have a problem, that the browser doent opens any page with htm and html extentions.. can u help me to findout this
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi Jitesh
Once upon a time I found a Java code to launch a system's default browser. That should work for you. I will have to dig up my archive to find that out and forward it to you if you want. Please let me know if you want me to forward that to you.
Regards
Maulin
Jithesh Kozhipurath
Ranch Hand

Joined: Aug 27, 2003
Posts: 76
Hai Maulin Vasavada,
Thanks, opening the default browser is not my problem, its working perfectly. but the default browser is not opening any files with htm and html extentions.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi Jitesh
So, what error you get? "Page not found" sort of?
Regards
Maulin
Jithesh Kozhipurath
Ranch Hand

Joined: Aug 27, 2003
Posts: 76
hai Maulin Vasavada,
when i try to open a page it displays a message box with error,

Unable to Open "<< Website Address >>"
[ September 09, 2003: Message edited by: Jithesh Kozhipurath ]
Jithesh Kozhipurath
Ranch Hand

Joined: Aug 27, 2003
Posts: 76
The code i used for opening a url ,
Process p = Runtime.getRuntime().exec(cmd);
Please help me to find the reason why its not opening htm & html files
ramdas pradeep kumar
Ranch Hand

Joined: Aug 27, 2003
Posts: 36
Hai,
I also tried it. I got the same error message. 'Cannot open *.htm'
pradeep kumar
Ashok Mash
Ranch Hand

Joined: Oct 13, 2000
Posts: 1936
I don't know if this will fix your problem, but I have this working.

Similarly, this is also working.


[ flickr ]
Jithesh Kozhipurath
Ranch Hand

Joined: Aug 27, 2003
Posts: 76
Hai ashok,
If i am using rundll file its not opening the htm and html files, if i use the path of IE it wont be the same for all the machines. So is there any other way to use the code for all the OS.
allibaba
Greenhorn

Joined: Sep 16, 2003
Posts: 1
import java.util.*;
import java.io.*;
public class GoodExecBrowser
{
public static void main(String args[])
{
if (args.length < 1)
{
System.out.println("USAGE:GoodExecBrowser <web-page.html>");
System.exit(1);
}

try
{
Runtime rt = Runtime.getRuntime();
String cmd = "C:/Program Files/Internet Explorer/Iexplore.exe " + args[0];
System.out.println("Execing " + cmd);
Process proc = rt.exec(cmd);
} catch (Throwable t)
{
t.printStackTrace();
}
}
}
i think its will help u but dont for get the argument this file having a argument of any html file
example
java GoodExecBrowser www.javarach.com


Hi again
Jithesh Kozhipurath
Ranch Hand

Joined: Aug 27, 2003
Posts: 76
Thanks,
But if i installed the Iexplore some where else how the program can identify the new path. Here you r using direct path c:\...... i dont think it will work on all platforms if i am using something other that windows how can i say its the correct path
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
What symptoms do you get with rundll32 not opening the page? This should work exactly like entering an html file name as a command in a dos box, or double-clicking in windows Explorer. Try all three techniques and see if you get the same results. It will fail for sure if there is no Windows association of "htm" or "html" extensions to "IE" program. Does the html filename include spaces? You might have to wrap it with quotes.
Jithesh Kozhipurath
Ranch Hand

Joined: Aug 27, 2003
Posts: 76
Hai friends,
If i try the direct path for iexplore the file opens. but when i try that with rundll it displays error. so the problem is not because of space, and even if i change the spaces to quotes then also the same error occurs. the problem is not only in my machine. i tried in another system and the same error happens. my statement for the execution is
rundll32 url.dll,FileProtocolHandler <<url>>
Jithesh Kozhipurath
Ranch Hand

Joined: Aug 27, 2003
Posts: 76
hai stan,
Could u tell how the rundll works
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
rundll can launch an arbitrary dll. You'd have to scour Microsoft docs to see what the dll that's being run does. But it seems to do exactly what happen when you dbl click a file in Windows Explorer. You can enter the rundll command in a command prompt box to test it. Try it with different file extensions and see it launch the program associated with the extension, like MSWord, Excel, etc.
Jithesh Kozhipurath
Ranch Hand

Joined: Aug 27, 2003
Posts: 76
Hai,
I am using a url.dll along with the rundll. when i typed the code in run window it is working. So that i can open page with my default browser. But still i am not able to rectify my problem because it is something different. ie, the case of htm and html.
Now what i did is just executing the rundll command and if an exception happens it tries to open with the path c:\...\iexplore so now the problem is solved but unalbe to solve with rundll
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
You need this:
http://browserlauncher.sourceforge.net
It's an open source, multi-platform browser launcher. Works great and it only one line to launch a browser:

brian


My Java Freeware:<br />MACCC - <a href="http://maccc.pipasoft.com" target="_blank" rel="nofollow">http://maccc.pipasoft.com</a><br />Nator - <a href="http://nator.pipasoft.com" target="_blank" rel="nofollow">http://nator.pipasoft.com</a>
Jithesh Kozhipurath
Ranch Hand

Joined: Aug 27, 2003
Posts: 76
Thanks Brian,
Its working. But this one is a big code to process each and every browser seperatly. Its really tedious and time consuming.

Thanks a lot.
Jithesh
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
But this one is a big code to process each and every browser seperatly. Its really tedious and time consuming.

You lost me there. I *think* you're saying that you looked at the BrowserLauncher code and it's a lot more than one or two lines to get a Browser to launch on various platforms and with various browsers. Is that what you're saying? If so, I agree - that's why it's a great resource to be able to use that code and only call one line to get a Browser launched. I've been using it sinc way before it was a Sourceforge project and it's worked on every machine I have ever tried it on.
Brian
Jithesh Kozhipurath
Ranch Hand

Joined: Aug 27, 2003
Posts: 76
hai brian
Thanx a lot. Now the problem is solved.
Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Opening a Browser From Java
 
Similar Threads
Opening Browser Window
Session Merging - URL Rewrite
sessiontimeout
Calling a JSP using Swing
Running Terminal commands from Java