• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Opening a Browser From Java

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can write
Runtime.exec("Iexplore http://www.yahoo.com");
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24206
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Jithesh Kozhipurath
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jitesh
So, what error you get? "Page not found" sort of?
Regards
Maulin
 
Jithesh Kozhipurath
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai,
I also tried it. I got the same error message. 'Cannot open *.htm'
pradeep kumar
 
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know if this will fix your problem, but I have this working.

Similarly, this is also working.
 
Jithesh Kozhipurath
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Jithesh Kozhipurath
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai stan,
Could u tell how the rundll works
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Jithesh Kozhipurath
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai brian
Thanx a lot. Now the problem is solved.
Thanks.
 
Think of how dumb the average person is. Mathematically, half of them are EVEN DUMBER. Smart tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic