Solved

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

Posted on 2004-08-27
15
223 Views
Last Modified: 2010-03-31
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
Comment
Question by:kibkid
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +3
15 Comments
 
LVL 92

Accepted Solution

by:
objects earned 65 total points
ID: 11918668
use runtime.exec() to start your browser
0
 
LVL 18

Expert Comment

by:armoghan
ID: 11919946
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
 
LVL 35

Expert Comment

by:girionis
ID: 11920738
http://javaalmanac.com/egs/java.lang/Exec.html where in the "command" give the command you use to open the browser.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:armoghan
ID: 11920825
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
 

Author Comment

by:kibkid
ID: 11922204
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
 
LVL 35

Expert Comment

by:girionis
ID: 11922285
http://javaalmanac.com/egs/javax.swing.text/ep_ShowHTML.html

Just bare in mind that JEditorPane does not support all the HTML tags.
0
 

Expert Comment

by:Behmen
ID: 11922324
You could use this browser opener API. It supports many OS´s and is freeware.

http://ostermiller.org/utils/Browser.html
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 11927421
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
 
LVL 9

Assisted Solution

by:DrWarezz
DrWarezz earned 60 total points
ID: 11927488
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
 
LVL 9

Expert Comment

by:DrWarezz
ID: 11927504
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
 
LVL 18

Expert Comment

by:armoghan
ID: 11928248
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
 

Expert Comment

by:Behmen
ID: 11929368
>  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
 
LVL 9

Expert Comment

by:DrWarezz
ID: 11929708
>"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
 

Author Comment

by:kibkid
ID: 11965691
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
 
LVL 92

Expert Comment

by:objects
ID: 11969006
thanks mate :)
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
Suggested Courses

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question