How to force an html document to always be opened by the browser?

Asked

Viewed 1,047 times

2

I’m developing a java application for a college job and have a help section for the user where I open a page html in the browser, or at least should open in this.

The issue is that if files with extension .html were by default of the system to be opened by a browser, all right it will be opened by the browser, but if this extension is by default linked to, for example, a text editor it will open in the text editor revealing the code.

I wonder if there is a java mode of a document html only be opened by the browser. The code I use is the below:

Desktop desktop = null;
desktop = Desktop.getDesktop();
URI url = null;

File file = new File("src/ajuda/index.html");

try {

    url = new URI(file.toURI().toString());
    desktop.browse(url);

} catch (URISyntaxException | IOException ex) {
    Logger.getLogger(Teste.class.getName()).log(Level.SEVERE, null, ex);
}
  • And in case you would assume that the computer has a default program? type everyone has IE? in this case you could use Runtime.getRuntime(). exec(""C: Program Files Internet Explorer iexplore.exe" www.google.com");

2 answers

3

I don’t know about java, but I program in other languages. I don’t know where you’re running your code, but I don’t know if it’s possible......

And if instead of you giving a command letting the OS decide where to open your HTML, you called the browser directly, passing as parameter the path of html, like this:

execute("c: programa file ie iexplorer.exe seuhtml.html").

This opens your HTML always in the browser. If you have a server running on the machine, just call the default path of your html:

execute("c: file ie iexplorer.exe programs http://localhost/seuhtml.html").

It’s an idea.

2

I don’t think this is possible mainly because, by doing this, you would be taking away the power of the user to control the actions that happen on their own computer. Following usability standards, the user should choose how and with which programs he will open/view a particular file type. What you can do is show messages advising the best way to open/view the file;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.