Solved

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

Posted on 2004-08-27
15
185 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
  • 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now