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 Desktop.open(File 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.*;
import java.io.File;
import java.io.IOException;

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.
            desktop.open(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

How do I launch user-default mail client application?

Here is an example using the java.awt.Desktop class to open user’s default mail client application. There are two methods provided, the mail() and the mail(URI uri) methods.

When specifying the URI to the application will be opened with the message field filled with the mailto information. You can refer to the following document for the valid mailto URI scheme http://www.ietf.org/rfc/rfc2368.txt

package org.kodejava.example.awt;

import java.awt.*;
import java.io.IOException;
import java.net.URI;

public class RunningDefaultMailClient {
    public static void main(String[] args) {
        // Get an instance of Desktop. If the platform doesn't support Desktop API an 
        // UnsupportedOperationException will be thrown.
        Desktop desktop = Desktop.getDesktop();

        try {
            // Open user-default mail client application.
            desktop.mail();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            // Open user-default mail client with the email message fields information.
            String message = "mailto:dummy@example.com?cc=test@example.com&subject=First%20Email";
            URI uri = URI.create(message);
            desktop.mail(uri);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}