Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Open HTML file using Java?

Tags:

I try to open HTML file from local (In my system) by Java program. I tried some of the program got through stack overflow but its not working as much.

For E.G.: I have this small HTML file.

<html>   <head>      Test Application   </head>   <body>      This is test application   </body> </html> 

My Java code:

Runtime rTime = Runtime.getRuntime(); String url = "D:/hi.html"; String browser = "C:/Program Files/Internet Explorer/iexplore.exe "; Process pc = rTime.exec(browser + url); pc.waitFor(); 

Any solution or tips appreciated.

like image 404
Wanna Coffee Avatar asked Dec 11 '13 11:12

Wanna Coffee


People also ask

Can Java read HTML file?

In java, we can extract the HTML content and can parse the HTML Document.

How do you use HTML in Java?

Put a tag like $tag for any dynamic content and then do something like this: File htmlTemplateFile = new File("path/template. html"); String htmlString = FileUtils. readFileToString(htmlTemplateFile); String title = "New Page"; String body = "This is Body"; htmlString = htmlString.

How do I open a HTML file?

Open the saved HTML file in your favorite browser (double click on the file, or right-click - and choose "Open with").


Video Answer


2 Answers

I would prefer to use default browser

File htmlFile = new File(url); Desktop.getDesktop().browse(htmlFile.toURI()); 
like image 154
RamonBoza Avatar answered Sep 16 '22 12:09

RamonBoza


Here is the code for a method that fails gracefully.

Note that the string can be the location of an html file.

/** * If possible this method opens the default browser to the specified web page. * If not it notifies the user of webpage's url so that they may access it * manually. *  * @param url *            - this can be in the form of a web address (http://www.mywebsite.com) *            or a path to an html file or SVG image file e.t.c  */ public static void openInBrowser(String url) {     try         {             URI uri = new URL(url).toURI();             Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;             if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE))                 desktop.browse(uri);         }     catch (Exception e)         {             /*              *  I know this is bad practice               *  but we don't want to do anything clever for a specific error              */             e.printStackTrace();              // Copy URL to the clipboard so the user can paste it into their browser             StringSelection stringSelection = new StringSelection(url);             Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();             clpbrd.setContents(stringSelection, null);             // Notify the user of the failure             WindowTools.informationWindow("This program just tried to open a webpage." + "\n"                 + "The URL has been copied to your clipboard, simply paste into your browser to access.",                     "Webpage: " + url);         } } 
like image 32
Troyseph Avatar answered Sep 20 '22 12:09

Troyseph