JavaFx helloworld 坑

发布时间 2023-11-23 19:09:06作者: Inshua

系统 Linux Mint

IDEA 创建的 hello world 项目,用 IDEA 运行就僵住,然而用 mvn clean javafx:run 却能成功————在系统 terminal能成功,在 IDEA 的 Terminal 不行。

不过我也是见怪不怪了,上次 Jetbrains 家的 Rider 运行 Avalonia 能出窗口,但是窗口是花的,到 Avalonia 报了 issue,最后尴尬的发现 dotnet build 后直接在系统 terminal 一切正常。

排查发现在 com.sun.prism.es2.ES2Pipeline 的静态初始化块中调用的 com.sun.prism.es2.X11GLFactory#initialize 函数会僵住,当执行 Class.forName("com.sun.prism.es2.ES2Pipeline") 就会执行到该静态块。

可能所有的 OpenGL 在 IDEA 都容易出问题。

最后在 PrismSettings 找到可以通过设置 tryOrder 避开 ES2,而使用其它的 Pipeline。javafx 共提供了 3 种 Pipeline,缩写分别是 es2 sw j2d,它默认会尝试 es2 sw。

设置 vm 参数为 -Dprism.order=sw,j2d,顺利出现窗口。

j2d 也可以用,但会有一段警告建议用 sw:

WARNING: The prism-j2d pipeline should not be used as the software
fallback pipeline. It is no longer tested nor intended to be used for
on-screen rendering. Please use the prism-sw pipeline instead by setting
the "prism.order" system property to "sw" rather than "j2d".

两个效果差不多。