Opening an external html file through Java

Q. How do i invoke an external application(web browser) to display an html page?

A. Perseverance and Patience are those which helped me to reach the solution to this problem. I spent more than 4 hours to find a solution, encountering many tips in the web, only to find at the end that all it takes is to pay attention to  subtle facts like “spaces” and “slashes”.

Here is how you get things to work.
First,  create an html file and name it as “sample.html“. Place it anywhere you  wish.
Next, you need to code the following:

Here, as you would have observed, browser contains the path to Internet Explorer browser and url contains the path to sample.html.

What you would not have observed is the “space” i have left in the browser path, at the end, after iexplore.exe.

This is what that matters and not placing this space is what makes many programmers break their heads making them think that runtime.exec doesn’t work. Also note the slash in the file path and browser path. I have used  forward slash at all instances. This would be obvious to you from the error message you get when you try to replace any of them  with backward slash.

Thats it!

Just run the code and enjoy!

Pass this tip to your colleagues!

4 comments on “Opening an external html file through Java

  1. how to save the page after opening with iexplorer knowing the the page is going to change using javascript code

  2. Hi i am getting this error –
    java.io.IOException: Cannot run program “C:\Program”: CreateProcess error=2, The system cannot find the file specified

    Runtime rTime=Runtime.getRuntime();
    String browser=”C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe “;
    String url= “http://localhost:8080/DL8thOct/faces/index0.xhtml”;

    Process pc=rTime.exec(“browser+url”);
    pc.waitFor();

    Its not taking space into account.
    I am using Netbeans 7.3.1 and JDK1.7. Any idea whats wrong in this?

  3. Pingback: Invoking an HTML page from JAVA codeCopyQuery CopyQuery | Question & Answer Tool for your Technical Queries,CopyQuery, ejjuit, query, copyquery, copyquery.com, android doubt, ios question, sql query, sqlite query, nodejsquery, dns query, update query,

Leave a comment