Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从JavaFX打开外部应用程序_Java_Javafx 8_External Application - Fatal编程技术网

从JavaFX打开外部应用程序

从JavaFX打开外部应用程序,java,javafx-8,external-application,Java,Javafx 8,External Application,我找到了一种使用HostServices在默认浏览器上打开链接的方法 getHostServices().showDocument("http://www.google.com"); 有没有办法在默认媒体播放器中打开媒体 是否有办法启动特定的文件或应用程序 一般来说,您可以使用以本机方式打开文件,如下所示: final Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null; if (deskt

我找到了一种使用HostServices在默认浏览器上打开链接的方法

getHostServices().showDocument("http://www.google.com");
  • 有没有办法在默认媒体播放器中打开媒体
  • 是否有办法启动特定的文件应用程序

一般来说,您可以使用以本机方式打开文件,如下所示:

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");
}
启动关联的应用程序以打开文件。如果指定 文件是一个目录,当前平台的文件管理器是 启动以打开它

更具体地说,对于浏览器,您可以直接使用,如下所示:

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");
}
启动默认浏览器以显示
URI
。如果使用默认浏览器 无法处理指定的
URI
,应用程序已注册 用于处理指定类型的uri。应用程序是 根据由定义的
URI的协议和路径确定
URI
class。如果调用线程没有必要的 权限,这是从小程序中调用的, 使用了
AppletContext.showDocument()
。类似地,如果调用 没有必要的权限,这是从 Java Web启动的应用程序,使用了
BasicService.showDocument()


如果您想在浏览器中打开具有
http:
方案的URL,或者使用该文件类型的默认应用程序打开文件,您引用的
HostServices.showDocument(…)
方法提供了一种“纯JavaFX”方法。请注意,您不能使用此功能(据我所知)从web服务器下载文件并使用默认应用程序打开它

要使用默认应用程序打开文件,必须将文件转换为
文件的字符串表示形式:
URL。下面是一个简单的例子:

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);
    }
}
导入java.io.File;
导入javafx.application.application;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.geometry.Insets;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.Label;
导入javafx.scene.control.TextField;
导入javafx.scene.layout.HBox;
导入javafx.scene.layout.VBox;
导入javafx.stage.FileChooser;
导入javafx.stage.stage;
公共类OpenResourceNative扩展应用程序{
@凌驾
公共无效开始(阶段primaryStage){
TextField TextField=新的TextField(“http://stackoverflow.com/questions/39898704");
按钮openURLButton=新按钮(“打开URL”);
EventHandler=e->open(textField.getText());
setOnAction(处理程序);
openURLButton.setOnAction(处理程序);
FileChooser FileChooser=newfilechooser();
按钮openFileButton=新建按钮(“打开文件…”);
openFileButton.setOnAction(e->{
File File=fileChooser.showOpenDialog(primaryStage);
如果(文件!=null){
打开(file.toURI().toString());
}
});
VBox根=新的VBox(5,
新的HBox(新标签(“URL”)、文本字段、openURLButton),
新HBox(openFileButton)
);
根。设置填充(新插图(20));
primaryStage.setScene(新场景(根));
primaryStage.show();
}
私有void open(字符串资源){
getHostServices().showDocument(资源);
}
公共静态void main(字符串[]args){
发射(args);
}
}
只有使用的解决方案可以从JavaFX打开文件

然而,一开始,我的应用程序被卡住了,我必须弄明白有必要从一个新线程调用。从当前线程或JavaFX应用程序线程调用该方法会导致应用程序无限期挂起,而不会引发异常

这是一个带有工作文件打开解决方案的小样本JavaFX应用程序:

导入java.awt.Desktop;
导入java.io.File;
导入java.io.IOException;
导入javafx.application.application;
导入javafx.concurrent.Task;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.stage.FileChooser;
导入javafx.stage.stage;
公共类FileOpenDemo扩展了应用程序{
@凌驾
公共无效开始(阶段primaryStage){
最终按钮=新按钮(“打开文件”);
按钮。设置操作(事件->{
最终文件选择器FileChooser=新文件选择器();
final File=fileChooser.showOpenDialog(primaryStage.getOwner());
if(file==null)
返回;
System.out.println(“所选文件:“+File.getName());
如果(!Desktop.isDesktopSupported()){
System.out.println(“不支持桌面”);
返回;
}
如果(!Desktop.getDesktop().isSupported(Desktop.Action.OPEN)){
System.out.println(“不支持文件打开”);
返回;
}
最终任务=新任务(){
@凌驾
public Void call()引发异常{
试一试{
Desktop.getDesktop().open(文件);
}捕获(IOE异常){
System.err.println(例如toString());
}
返回null;
}
};
最终线程=新线程(任务);
setDaemon(true);
thread.start();
});
设置场景(新场景(按钮));
primaryStage.show();
}
公共静态void main(字符串[]args){
发射(args);
}
}
另一个建议的解决方案根本不起作用。我在Ubuntu 17.10 amd64上使用OpenJFX 8u141,调用时出现以下异常:

java.lang.ClassNotFoundException:com.sun.deploy.uitoolkit.i