javafx 应用程序中 java.awt.Desktop.isDesktopSupported 返回 false

标签 java javafx

我有这个 javafx Controller 方法尝试打开由应用程序生成的本地 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();
    }
}

我的 Controller 类:

@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。我在另一个项目中又写了一个main函数,再次测试,返回true。

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

那么我在这里缺少什么?

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

最佳答案

当您已经使用 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。

我怀疑您遇到的一个问题是 Desktop.getDesktop().open() 是非阻塞的,因此您的应用程序会在桌面实际打开网页之前退出。

关于javafx 应用程序中 java.awt.Desktop.isDesktopSupported 返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61362829/

相关文章:

java - Caps Lock 键卡住 JavaFX 应用程序线程

java - 我可以使用Cucumber测试使用多种语言的应用程序吗?

java - 我怎样才能 "auto-adjust"javafx中的类别轴标签?

javafx - 如何在窗口最大化时自动调整 JavaFX 容器的大小?

java - 将来自 Primefaces 的 UploadedFile 的输入流转换为字符串

java - 如何让java程序只运行1秒?

java - 无法转发到请求的错误页面...因为已经提交了响应。结果,响应可能有错误的状态码

java - 由于更改 FXML 文件中的 Controller 导致线程出现异常

button - JavaFX 将两个按钮置于彼此下方

JavaFX 和 "MediaException: UNKNOWN ... Could not create player!"