首页 > 解决方案 > java.awt.Desktop.isDesktopSupported 在 javafx 应用程序中返回 false

问题描述

我有这个 javafx 控制器方法试图打开由应用程序生成的本地 html 文件。该函数正在另一个线程中运行。

我使用 fxmlloader 启动应用程序的主类:

@SpringBootApplication
public class DemoAppiumProjectApplication extends Application
{

    public static ConfigurableApplicationContext applicationContext;

    public static void main(String[] args)
    {
        applicationContext = SpringApplication.run(DemoAppiumProjectApplication.class, args);
        Application.launch(DemoAppiumProjectApplication.class, args);
    }

    @Override
    public void start(Stage stage)
        throws Exception
    {
        stage.setTitle("JS SDK Demo Test Suite");
        stage.setScene(new Scene(createRoot()));
        stage.show();
    }

    private Parent createRoot()
        throws IOException
    {
        FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setLocation(DemoAppiumProjectApplication.class.getResource("/setup.fxml"));
        fxmlLoader.setControllerFactory(applicationContext::getBean);
        return fxmlLoader.load();
    }
}

我的控制器类:

@Override
    public void initialize(URL url, ResourceBundle resourceBundle)
    {
        Task<String> task = new Task<String>()
        {
            @Override
            protected String call()
                throws Exception
            {
                showFinalDialog();
                return null;
            }
        };

        new Thread(task).start();
    }

public void showFinalDialog()
    {
        Platform.runLater(() -> {
            String text = "Test is complete! Click to view test report.";
            Alert alert = new Alert(Alert.AlertType.INFORMATION, text, ButtonType.OK);
            alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
            Optional<ButtonType> buttonType = alert.showAndWait();
            if (!buttonType.isPresent())
            {
                Platform.exit();
            }
            else if (buttonType.get() == ButtonType.OK)
            {
                if (Desktop.isDesktopSupported())
                {
                    try
                    {
                        Desktop.getDesktop()
                            .open(new File(System.getProperty("user.dir") + "/test-output/ExtentReport.html"));
                    }
                    catch (IOException e)
                    {
                        createAlertWindow(e.getMessage());
                    }
                }
                Platform.exit();
            }
        });
    }

现在唯一的问题是条件 Desktop.isDesktopSupported 在此处返回 false。我在另一个项目中写了另一个主函数再次测试,它返回true。

我想这意味着我的操作系统(Windows 10)支持桌面,但我的 javafx 应用程序不支持桌面?

那么我在这里错过了什么?

也许还有另一种推荐的方式来打开这个 html 文件?任何想法都会有所帮助!谢谢!

标签: javajavafx

解决方案


当您已经使用 javafx 时,为什么还要使用桌面打开本地 html 文件。

这是一个例子。

public class HelpWindow extends Application{

    @Override
    public void start(Stage primaryStage) throws Exception{
        FlowPane root = new FlowPane();

        primaryStage.setTitle("Help Window");

        WebView view = new WebView();
        WebEngine engine = view.getEngine();
        engine.load( HelpWindow.class.getResource("help.html").toString());
        root.getChildren().add(view);

        Scene scene = new Scene(root, 790, 675);
        primaryStage.setScene(scene);
        primaryStage.show();
        System.out.println(Desktop.isDesktopSupported());
    }    
}

这将显示help.html我作为资源包含的文件。从外观上看,您可能正在生成 .html 文件,因此您必须以不同的方式获取该文件。

<html>
<body>
 <p> Here is some text</p>
 <a href="https://stackoverflow.com/q/61362829/2067492">original question</a>
</body>
</html>

我还包括打印出Desktop.isDesktopSupported()对我来说返回 true 的 since 。

我怀疑你遇到的一个问题是Desktop.getDesktop().open()非阻塞的,所以你的应用程序在你的桌面可以真正打开网页之前就退出了。


推荐阅读