Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Introduction

In various Java applications, HTML files are often needed to be programmatically opened and displayed. Java provides several methods to accomplish this task, whether it’s for generating reports, displaying documentation, or presenting user interfaces.

In this tutorial, we’ll explore two different approaches: using the Desktop and ProcessBuilder classes.

2. Using the Desktop Class

The Desktop class provides a platform-independent way to interact with the desktop’s default browser.

Before we delve into the approaches, let’s initialize the URL and absolute HTML file path. Let’s first ensure that the HTML file exists and obtain its absolute path for further use in our tests:

public URL url;
public String absolutePath;
url = getClass().getResource("/test.html");
assert url != null;
File file = new File(url.toURI());
if (!file.exists()) {
    fail("HTML file does not exist: " + url);
}
absolutePath = file.getAbsolutePath();

In this initialization block, we first obtain the URL of the test.html HTML file using the getClass().getResource() method. We then assert that the URL is not null to ensure the file exists.

Next, we convert the URL to a File object and obtain its absolute path using the toURI() method. If the file doesn’t exist, the test fails.

Now, let’s open an HTML file using the Desktop class:

@Test
public void givenHtmlFile_whenUsingDesktopClass_thenOpenFileInDefaultBrowser() throws IOException {
    File htmlFile = new File(absolutePath);
    Desktop.getDesktop().browse(htmlFile.toURI());
    assertTrue(true);
}

In this approach, we create a File object representing the HTML file and use Desktop.getDesktop().browse(htmlFile.toURI()) to open it. After attempting to open the file, we use the assertTrue() method to verify that the operation was completed successfully.

3. Using the ProcessBuilder Class

ProcessBuilder allows us to execute operating system commands. Here’s how we can open an HTML file using ProcessBuilder:

@Test
public void givenHtmlFile_whenUsingProcessBuilder_thenOpenFileInDefaultBrowser() throws IOException {
    ProcessBuilder pb;
    if (System.getProperty("os.name").toLowerCase().contains("win")) {
        pb = new ProcessBuilder("cmd.exe", "/c", "start", absolutePath);
    } else {
        pb = new ProcessBuilder("xdg-open", absolutePath);
    }
    pb.start();
    assertTrue(true);
}

In this approach, we construct a ProcessBuilder instance tailored to the operating system’s requirements for opening the HTML file.

On Windows systems, we specify the command (“cmd.exe“, “/c“, “start”), which initiates the default browser with the HTML file. Conversely, we employ “xdg-open“, a command designed to launch the default web browser on non-Windows platforms.

Subsequently, we invoke the pb.start() method to commence the process, thereby opening the HTML file in the appropriate default browser based on the underlying operating system.

4. Conclusion

In conclusion, whether opting for the simplicity of the Desktop class or the flexibility of ProcessBuilder, Java offers multiple ways to open HTML files programmatically. These methods empower developers to seamlessly integrate HTML content into their Java applications, enhancing the user experience and functionality.

As always, the complete code samples for this article can be found over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.