Link to home
Start Free TrialLog in
Avatar of kibkid
kibkid

asked on

Can Java access a website link provided, and open that page in a browser?

Hi can I write a program that given a URL will be able to open that URL in a browser?
If so what would be the code to open the URL?

thanx guys.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can use a JEditorPane and HTMLEditorKit if wnat to show the page inside java application
JEditorPane editor=new JEditorPane();
editor.setEditable(false);
editor.setEditorKitForContentType("text/html", new HTMLEditoKit());
editor.setPage(new URL("www.yahoo.com"));


http://javaalmanac.com/egs/java.lang/Exec.html where in the "command" give the command you use to open the browser.
Only problem with Exec command is that it would be OS dependent

The work around for it is check for different OS;s and use the command accordingly

i.e. for Windows it may be
"iexplore www.yahoo.com"
and for Linux and other use the appropriate command in a Try catch or If blocks
Avatar of kibkid
kibkid

ASKER

Thanx very much guys, right now I'm leaning towards the JEditor thing, but how can I show the page? How can I read the information from the yahoo page and show it?
I'm running the code posted and it doesn't show anything.

I know it is not supposed to show anything as it is, I know I have to request the information, but how do I do it? Do I use streams?

thanx guys
http://javaalmanac.com/egs/javax.swing.text/ep_ShowHTML.html

Just bare in mind that JEditorPane does not support all the HTML tags.
You could use this browser opener API. It supports many OS´s and is freeware.

http://ostermiller.org/utils/Browser.html
So, lets summarize....

>"Just bare in mind that JEditorPane does not support all the HTML tags."
..Most of the modern websites use all this fancy XHTML, then you have all the other web languages, etc.. So, you could only load a limited number of sites this way..

>"http://ostermiller.org/utils/Browser.html"
You could use this. However, you'd probably be better off just to use objects idea of: "use runtime.exec() to start your browser"

Start the browser with that, then just pass the URL as an argument to it. Like so:

    try {
        // Execute a command with arguments
        String sURL = "https://www.experts-exchange.com/";
        String command = "c:\program files\internet explorer\iexplore.exe " + sURL;

        Process child = Runtime.getRuntime().exec(command);
    } catch (IOException e) {
        System.out.println("Excepton: " + e.getMessage() );
    }

See: http://www.javaalmanac.com/egs/java.lang/Exec.html

gL,
[r.D]
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Last post, I promise! :o)

Note that I changed:
c:\program files\internet explorer\iexplore.exe

to:
c:\\program files\\internet explorer\\iexplore.exe

(must use double backslashes). :-P

[r.D]
I think if you do not want to interact with the HTML code and your main objective is just to show the HTML page of "any Sort", then you should rely on native browsers.

Behmen suggested API will work for you.
Just add this code file in your project and use these two line
http://ostermiller.org/utils/Browser.java.html



// Initialize the class.
// Needs to be done once.
Browser.init();

// Display this page
Browser.displayURL("http://ostermiller.org/utils/Browser.html");
>  String command = "c:\\program files\\internet explorer\\iexplore.exe " + sURL;
>  Process child = Runtime.getRuntime().exec(command);

what if i dont have my browser installed in c:\program files\ ? Or iexplorer is not
my default browser ?

Under Windows you can use:
Runtime.getRuntime().exec('rundll32 url.dll,FileProtocolHandler http://www.experts-exchange.com');

But if you want support for other Operating Systems (not all), you should use: http://ostermiller.org/utils/Browser.html
its very easy to implement, only a 2-liner. There is a another artikel on Javaworld that has
a less powerfull source code. But nice to read.
http://www.javaworld.com/javaworld/javatips/jw-javatip66.html

If you really want support for all Operating Systems, you can use Suns Hotjava Browser:
java.sun.com/products/hotjava/3.0/
>"what if i dont have my browser installed in c:\program files\ ? Or iexplorer is not
my default browser ?"
Well, you'd change it to the appropriate one.
However, your code;  "Runtime.getRuntime().exec('rundll32 url.dll,FileProtocolHandler http://www.experts-exchange.com');"

Would obviously be better.  :o)

ta,
[r.D]
Avatar of kibkid

ASKER

Hi guys thank you very much for all the help. I would like to give you all points coz all of you helped in one way or another but unfortunately I can't. I ended up using objects Idea to open links with the bowser, however I also used DrWarez's code in one part of my program so I'll split the points between them.
Thanx to you all though I appreciate it.
thanks mate :)