结合使用JavaFX和Intellij IDEA [英] Using JavaFX with Intellij IDEA
问题描述
我完全按照教程进行操作,似乎无法正常使用.本教程位于JavaFX and Intellij
Non-modular from IDE
部分下: https://openjfx.io/openjfx- docs/#install-java
I've followed a tutorial precisely and I can't seem to get it to work. The tutorial is under JavaFX and Intellij
Non-modular from IDE
sections: https://openjfx.io/openjfx-docs/#install-java
这是我尝试运行默认的Intellij Idea JavaFX项目时收到的错误消息:
Here is the error message I receive when trying to run the default Intellij Idea JavaFX project:
"C:\Program Files\Java\jdk-11.0.1\bin\java.exe" --module-path %PATH_TO_FX% --add-modules=javafx.controls,javafx.fxml --add-modules javafx.base,javafx.graphics --add-reads javafx.base=ALL-UNNAMED --add-reads javafx.graphics=ALL-UNNAMED "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3\lib\idea_rt.jar=53491:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\jonat\IdeaProjects\Tawe-Lib FX\out\production\Tawe-Lib FX;C:\Program Files\Java\javafx-sdk-11.0.1\lib\src.zip;C:\Program Files\Java\javafx-sdk-11.0.1\lib\javafx-swt.jar;C:\Program Files\Java\javafx-sdk-11.0.1\lib\javafx.web.jar;C:\Program Files\Java\javafx-sdk-11.0.1\lib\javafx.base.jar;C:\Program Files\Java\javafx-sdk-11.0.1\lib\javafx.fxml.jar;C:\Program Files\Java\javafx-sdk-11.0.1\lib\javafx.media.jar;C:\Program Files\Java\javafx-sdk-11.0.1\lib\javafx.swing.jar;C:\Program Files\Java\javafx-sdk-11.0.1\lib\javafx.controls.jar;C:\Program Files\Java\javafx-sdk-11.0.1\lib\javafx.graphics.jar" sample.Main
Error occurred during initialization of boot layer
java.lang.module.FindException: Module javafx.base not found
Process finished with exit code 1
这对我来说意义不大,因为我可以在侧边栏的lib
下看到javafx.base
:
This makes little sense to me as I can see javafx.base
under lib
on the sidebar:
通向jdk-11.0.1
和javafx-sdk-11.0.1
的路径:
C:\ Program Files \ Java
C:\Program Files\Java
已安装Java:
C:\Users\jonat>java --version
openjdk 11.0.1 2018-10-16
OpenJDK Runtime Environment 18.9 (build 11.0.1+13)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)
设置了
JAVA_HOME
变量(在指南中提到):
JAVA_HOME
variable (mentioned in guide) is set:
C:\Users\jonat>echo %JAVA_HOME%
C:\Program Files\Java\jdk-11.0.1
设置了
PATH_TO_FX
变量:
C:\Users\jonat>echo %PATH_TO_FX%
C:\Program Files\Java\javafx-sdk-11.0.1\lib
我真的不知道从这里去哪里.我已经按照本教程进行了精确的操作,但是它不起作用.任何帮助将不胜感激,如果您需要更多信息,请对此发表评论.
I have really no idea where to go from here. I have followed the tutorial precisely, and it does not work. Any help would be greatly appreciated and if you require more info please just drop a comment about it.
推荐答案
基于发布的命令行,这是IntelliJ用于运行项目的内容:
Based on the posted command line, this is what IntelliJ applies to run your project:
"C:\Program Files\Java\jdk-11.0.1\bin\java.exe" --module-path %PATH_TO_FX% --add-modules=javafx.controls,javafx.fxml
因此,问题很明显:在VM选项中,您已设置 verbatim 教程说:
So the issue is quite clear: In the VM options you have set verbatim what the tutorial says:
但是您没有为PATH_TO_FX
应用您的真实路径,因为在教程中该命令后插入的图片中建议使用此:
But you haven't applied your real path for PATH_TO_FX
, as it is suggested in the picture inserted after that command in the tutorial:
IntelliJ不会解析该变量,并且未设置模块路径,因此会出现预期的错误,表明未找到JavaFX模块:
IntelliJ doesn't resolve that variable and the module path is not set, hence you get the expected error that reveals that the JavaFX modules are not found:
Error occurred during initialization of boot layer
java.lang.module.FindException: Module javafx.controls not found
解决方案
这可以通过两种方式解决:
This can be solved in two ways:
- 应用您的路径:
编辑运行配置,然后在VM选项中添加路径:
Edit run configurations, and in the VM options add your path:
--module-path "C:\Program Files\Java\javafx-sdk-11.0.1\lib" --add-modules=javafx.controls,javafx.fxml
应用,然后运行.应该可以.
Apply, and run. It should work.
- 添加环境变量
您还可以设置环境变量.转到IntelliJ->File->Settings->Appearance & Behavior->Path Variables
,然后添加PATH_TO_FX
,并带有lib
文件夹的路径:
You can also set an environment variable. Go to IntelliJ->File->Settings->Appearance & Behavior->Path Variables
, and add PATH_TO_FX
, with the path to the lib
folder:
然后您可以在VM选项中使用文字$PATH_TO_FX$
或${PATH_TO_FX}
:
And then you can use the literals $PATH_TO_FX$
or ${PATH_TO_FX}
in the VM options:
--module-path ${PATH_TO_FX} --add-modules=javafx.controls,javafx.fxml
应用并运行.
请注意,这是一种更永久的解决方案,可以应用于任何其他JavaFX项目.
Note that this is a more permanent solution that can be apply to any other JavaFX project.
这篇关于结合使用JavaFX和Intellij IDEA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!