7

I found a way to open a link on default browser using HostServices.

getHostServices().showDocument("http://www.google.com");
  • Is there any way to open a media in default media player?
  • Is there any way to launch a specific File or Application?
4
  • 1
    Whenever I've tried this with a URL (including a file:// URL) that maps to a particular document, it opens the document with the default application for that document type. Does it not work the same way for media, etc? It's probably better to avoid mixing AWT and JavaFX (which the accepted answer does) if you can reasonably do so.
    – James_D
    Oct 6, 2016 at 15:49
  • Yes, if it contains file:// I am facing IllegalArgumentException. I did not notice before reading your comment that this is from AWT. I would highly appreciate if you please share your answer how can I do this with JavaFX, which will enrich my knowledge & experience too.
    – Rana Depto
    Oct 6, 2016 at 17:52
  • Well, I just tested again, and it works with file:// URLs but won't work with other document types with http: URLs (it opens the browser, which downloads the file). I'll add an answer, but if you want to open media which are downloaded from a web server, I'm not sure there is a way to do it.
    – James_D
    Oct 6, 2016 at 17:54
  • Ok, it will not be a problem.
    – Rana Depto
    Oct 6, 2016 at 17:57

3 Answers 3

8

Generally speaking, you can use Desktop#open(file) to open a file natively as next:

final Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
if (desktop != null && desktop.isSupported(Desktop.Action.OPEN)) {
    desktop.open(file);
} else {
    throw new UnsupportedOperationException("Open action not supported");
}

Launches the associated application to open the file. If the specified file is a directory, the file manager of the current platform is launched to open it.

More specifically, in case of a browser you can use directly Desktop#browse(uri), as next:

final Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
    desktop.browse(uri);
} else {
    throw new UnsupportedOperationException("Browse action not supported");
}

Launches the default browser to display a URI. If the default browser is not able to handle the specified URI, the application registered for handling URIs of the specified type is invoked. The application is determined from the protocol and path of the URI, as defined by the URI class. If the calling thread does not have the necessary permissions, and this is invoked from within an applet, AppletContext.showDocument() is used. Similarly, if the calling does not have the necessary permissions, and this is invoked from within a Java Web Started application, BasicService.showDocument() is used.

0
5

If you want to either open a URL which has an http: scheme in the browser, or open a file using the default application for that file type, the HostServices.showDocument(...) method you referenced provides a "pure JavaFX" way to do this. Note that you can't use this (as far as I can tell) to download a file from a web server and open it with the default application.

To open a file with the default application, you must convert the file to the string representation of the file: URL. Here is a simple example:

import java.io.File;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class OpenResourceNatively extends Application {

    @Override
    public void start(Stage primaryStage) {
        TextField textField = new TextField("http://stackoverflow.com/questions/39898704");
        Button openURLButton = new Button("Open URL");
        EventHandler<ActionEvent> handler = e -> open(textField.getText());
        textField.setOnAction(handler);
        openURLButton.setOnAction(handler);

        FileChooser fileChooser = new FileChooser();
        Button openFileButton = new Button("Open File...");
        openFileButton.setOnAction(e -> {
            File file = fileChooser.showOpenDialog(primaryStage);
            if (file != null) {
                open(file.toURI().toString());
            }
        });

        VBox root = new VBox(5, 
                new HBox(new Label("URL:"), textField, openURLButton),
                new HBox(openFileButton)
        );

        root.setPadding(new Insets(20));
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

    private void open(String resource) {
        getHostServices().showDocument(resource);
    }

    public static void main(String[] args) {
        launch(args);
    }
}
4
  • Ok, now I got it. HostServices.showDocument(...) method can not only open a browser only, but also can open any kind of file with operating systems installed default application. Thanks a lot :)
    – Rana Depto
    Oct 6, 2016 at 18:12
  • In AWT there was a way to open directory in System Explorer. In JavaFX how to open a directory in System Explorer?
    – Rana Depto
    Oct 6, 2016 at 18:15
  • @RanaDepto Same thing works, doesn't it? If file refers to a directory, getHostServices().showDocument(file.toURI().toString()) will open the directory in the system file browser.
    – James_D
    Oct 6, 2016 at 18:18
  • Using OpenJFX 8u141 on Ubuntu 17.10 amd64, calling HostServices#showDocument(String uri) caused the following exception to be thrown: java.lang.ClassNotFoundException: com.sun.deploy.uitoolkit.impl.fx.HostServicesFactory Dec 27, 2017 at 19:14
4

Only the solution with java.awt.Desktop worked for me to open a file from JavaFX.

However, at first, my application got stuck and I had to figure out that it is necessary to call Desktop#open(File file) from a new thread. Calling the method from the current thread or the JavaFX application thread Platform#runLater(Runnable runnable) resulted in the application to hang indefinitely without an exception being thrown.

This is a small sample JavaFX application with the working file open solution:

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class FileOpenDemo extends Application {

    @Override
    public void start(Stage primaryStage) {
        final Button button = new Button("Open file");

        button.setOnAction(event -> {
            final FileChooser fileChooser = new FileChooser();
            final File file = fileChooser.showOpenDialog(primaryStage.getOwner());

            if (file == null)
                return;

            System.out.println("File selected: " + file.getName());

            if (!Desktop.isDesktopSupported()) {
                System.out.println("Desktop not supported");
                return;
            }

            if (!Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) {
                System.out.println("File opening not supported");
                return;
            }

            final Task<Void> task = new Task<Void>() {
                @Override
                public Void call() throws Exception {
                    try {
                        Desktop.getDesktop().open(file);
                    } catch (IOException e) {
                        System.err.println(e.toString());
                    }
                    return null;
                }
            };

            final Thread thread = new Thread(task);
            thread.setDaemon(true);
            thread.start();
        });

        primaryStage.setScene(new Scene(button));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

The other proposed solution with javafx.application.HostServices did not work at all. I am using OpenJFX 8u141 on Ubuntu 17.10 amd64 and I got the following exception when invoking HostServices#showDocument(String uri):

java.lang.ClassNotFoundException: com.sun.deploy.uitoolkit.impl.fx.HostServicesFactory

Obviously, JavaFX HostServices is not yet properly implemented on all platforms. On this topic see also: https://github.com/Qabel/qabel-desktop/issues/420

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.