【问题标题】:JavaFX program does not run when VM Options set to one directory, but runs fine when set to another directory with the same files当 VM 选项设置为一个目录时 JavaFX 程序不运行,但设置为具有相同文件的另一个目录时运行良好
【发布时间】:2018-11-13 18:36:30
【问题描述】:

我有一个包含两个模块的 IntelliJ (Java 11) 项目。一个是简单的 java 模块,另一个是 JavaFX 程序。这些都是空的,因为我正在设置项目结构以在 Git 上使用并试图摆脱任何绝对文件路径。

项目的根目录是一个 lib 文件夹,其中包含 JavaFX 的所有 JAR(以及 JUnit 的其他一些 JAR)。当我第一次使用框架时,这些是直接从我最初将 JavaFX 提取到的文件夹中复制的(在构建非 Git JavaFX 项目时我仍然可以正常使用)。

我在项目中添加了这些 JAR 作为依赖项。任何使用它们的代码在 IDE 中都显示没有错误。我知道要运行 JavaFX 项目,我必须将 --module-path <path to JavaFX libs> --add-modules=javafx.controls,javafx.fxml 添加到运行配置中的 VM 选项中。这就是问题所在。

当我使用前面提到的 JavaFX 原始提取位置的路径时,一切正常(即程序窗口打开):--module-path C:/Tools/JavaFX/lib/ --add-modules=javafx.controls,javafx.fxml。

但是,当我在项目中使用包含相同文件(加上 JUnit 文件)的 lib 文件夹的路径时,它不起作用(出现运行时错误并且程序不显示其窗口):--module-path C:/Users/Henry/Documents/IntelliJ/LAA_Rotas_4/lib/ --add-modules=javafx.controls,javafx.fxml

以下是运行时输出:

"C:\Program Files\Java\jdk-11\bin\java.exe" --module-path C:/Users/Henry/Documents/IntelliJ/LAA_Rotas_4/lib/ --add-modules=javafx.controls,javafx.fxml "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.2.4\lib\idea_rt.jar=57474:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.2.4\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\Henry\Documents\IntelliJ\LAA_Rotas_4\out\production\Demo;C:\Users\Henry\Documents\IntelliJ\LAA_Rotas_4\lib\apiguardian-api-1.0.0.jar;C:\Users\Henry\Documents\IntelliJ\LAA_Rotas_4\lib\javafx-swt.jar;C:\Users\Henry\Documents\IntelliJ\LAA_Rotas_4\lib\javafx.base.jar;C:\Users\Henry\Documents\IntelliJ\LAA_Rotas_4\lib\javafx.controls.jar;C:\Users\Henry\Documents\IntelliJ\LAA_Rotas_4\lib\javafx.fxml.jar;C:\Users\Henry\Documents\IntelliJ\LAA_Rotas_4\lib\javafx.graphics.jar;C:\Users\Henry\Documents\IntelliJ\LAA_Rotas_4\lib\javafx.media.jar;C:\Users\Henry\Documents\IntelliJ\LAA_Rotas_4\lib\javafx.swing.jar;C:\Users\Henry\Documents\IntelliJ\LAA_Rotas_4\lib\javafx.web.jar;C:\Users\Henry\Documents\IntelliJ\LAA_Rotas_4\lib\junit-jupiter-api-5.3.1.jar;C:\Users\Henry\Documents\IntelliJ\LAA_Rotas_4\lib\junit-platform-commons-1.3.1.jar;C:\Users\Henry\Documents\IntelliJ\LAA_Rotas_4\lib\opentest4j-1.1.1.jar net.atos.laa_rotas.demo.Main
Graphics Device initialization failed for :  d3d, sw
Error initializing QuantumRenderer: no suitable pipeline found
java.lang.RuntimeException: java.lang.RuntimeException: Error initializing QuantumRenderer: no suitable pipeline found
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumRenderer.getInstance(QuantumRenderer.java:280)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.init(QuantumToolkit.java:222)
    at javafx.graphics/com.sun.javafx.tk.Toolkit.getToolkit(Toolkit.java:260)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:267)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:158)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.startToolkit(LauncherImpl.java:658)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:409)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Error initializing QuantumRenderer: no suitable pipeline found
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.init(QuantumRenderer.java:94)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(QuantumRenderer.java:124)
    at java.base/java.lang.Thread.run(Thread.java:834)
Exception in thread "main" java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: No toolkit found
    at javafx.graphics/com.sun.javafx.tk.Toolkit.getToolkit(Toolkit.java:272)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:267)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:158)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.startToolkit(LauncherImpl.java:658)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:409)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    ... 5 more

Process finished with exit code 1

在内部,在 IntelliJ 的 workspace.xml 文件中,这些 VM 选项被转换为 --module-path $PROJECT_DIR$/lib/ --add-modules=javafx.controls,javafx.fxml。但是,即使我在workspace.xml 文件中直接将该路径设置为C:/Users/Henry/Documents/IntelliJ/LAA_Rotas_4/lib/ 并重新加载并运行该项目,也没有任何变化。

有谁知道为什么会发生这种情况以及可以做些什么来解决它?

【问题讨论】:

  • JavaFX SDK for Windows 包含 lib 文件夹中的 jars 以及 bin 文件夹中的本机 dll 库,这就是原始路径适合您的原因。
  • @JoséPereda 哇,谢谢!那么在我的项目中包含 JavaFX 的推荐方法是什么?罐子不是让程序可以跨平台吗?要求 dll 只会将其绑定到 Windows。我是否需要下载仅使用 jar 的其他跨平台版本?
  • 没有“只用jar”的版本,每个平台都有一些原生库。查看起始guide 和不同的相关samples,并找到适合您情况的场景。

标签: java intellij-idea javafx


【解决方案1】:

我也遇到了同样的问题,但我找到了解决这个问题的方法,您只需将其添加到您的 intelliJ VM 选项中,

右键单击您的主类,然后单击修改运行配置,然后在您的 VMoptions 中添加以下行并确保替换目录

    --module-path
"C:\Users\your-directory\javafx-sdk-16\lib"
--add-modules
"javafx.controls,javafx.fxml"

并确保您在 lib 文件夹所在的同一目录中拥有 javafx sdk 的所有文件和目录

bin
合法
lib

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-25
    • 2013-07-27
    • 1970-01-01
    • 2013-02-22
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多