How do I open a file using the default registered application?

If you want to open a file using the default registered or associated application for those files you can use the file) method call. In the example below we’ll ask the Desktop class to open a text file.

package org.kodejava.example.awt;

import java.awt.*;

public class RunningDefaultAppOpen {
    public static void main(String[] args) {
        // A reference to a text file
        File file = new File("data.txt");

        try {
            Desktop desktop = Desktop.getDesktop();

            // Open a file using the default program for the file type. In the example 
            // we will launch a default registered program to open a text file. For 
            // example on Windows operating system this call might launch a notepad.exe 
            // to open the file.
        } catch (IOException e) {

Wayan Saryada

Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. If these posts help, you can support me, buy me a cup of coffee or tea. Thank you 🥳


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.