Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

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.
0
kibkid
Asked:
kibkid
  • 4
  • 3
  • 2
  • +3
2 Solutions
 
objectsCommented:
use runtime.exec() to start your browser
0
 
armoghanCommented:
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"));


0
 
girionisCommented:
http://javaalmanac.com/egs/java.lang/Exec.html where in the "command" give the command you use to open the browser.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
armoghanCommented:
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
0
 
kibkidAuthor Commented:
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
0
 
girionisCommented:
http://javaalmanac.com/egs/javax.swing.text/ep_ShowHTML.html

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

http://ostermiller.org/utils/Browser.html
0
 
DrWarezzCommented:
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 = "http://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]
0
 
DrWarezzCommented:
So, more specifically; a program to test this code:

//-----8<--------------------------- OpenUrl.java -------------------------------------------------
import java.io.*;

public class OpenUrl {
    public static void main ( String [] args ) {
        String sURL = "";
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            while (sURL != null) {
                System.out.print("URL: ");
                sURL = in.readLine();
            try {
                // Execute a command with arguments
                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() );
            }
        System.exit(0);
            }
        } catch (IOException e) {
            System.out.println("Excepton: " + e.getMessage() );
        }


    }
}
//-----8<--------------------------- OpenUrl.java -------------------------------------------------

gL,
[r.D]

;o) HeHeHe
0
 
DrWarezzCommented:
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]
0
 
armoghanCommented:
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");
0
 
BehmenCommented:
>  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/
0
 
DrWarezzCommented:
>"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]
0
 
kibkidAuthor Commented:
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.
0
 
objectsCommented:
thanks mate :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now