将 JavaFX 与 Intellij IDEA 结合使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53435349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:47:47  来源:igfitidea点击:

Using JavaFX with Intellij IDEA

javaintellij-ideajavafxjavafx-11

提问by Jonathan Woollett-light

I've followed a tutorial precisely and I can't seem to get it to work. The tutorial is under JavaFX and IntellijNon-modular from IDEsections: https://openjfx.io/openjfx-docs/#install-java

我精确地遵循了教程,但似乎无法使其正常工作。本教程位于以下JavaFX and IntellijNon-modular from IDE部分:https: //openjfx.io/openjfx-docs/#install-java

Here is the error message I receive when trying to run the default Intellij Idea JavaFX project:

这是我在尝试运行默认的 Intellij Idea JavaFX 项目时收到的错误消息:

"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

This makes little sense to me as I can see javafx.baseunder libon the sidebar: enter image description here

这对我来说毫无意义,因为我可以在侧边栏javafx.base下方看到lib在此处输入图片说明

The path leading to jdk-11.0.1and javafx-sdk-11.0.1:

通向jdk-11.0.1和的路径javafx-sdk-11.0.1

C:\Program Files\Java

C:\程序文件\Java

Java is installed:

安装了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_HOMEvariable (mentioned in guide) is set:

JAVA_HOME变量(在指南中提到)设置:

C:\Users\jonat>echo %JAVA_HOME%
C:\Program Files\Java\jdk-11.0.1

PATH_TO_FXvariable is set:

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.

我真的不知道从这里去哪里。我完全按照教程进行了操作,但它不起作用。任何帮助将不胜感激,如果您需要更多信息,请发表评论。

采纳答案by José Pereda

Based on the posted command line, this is what IntelliJ applies to run your project:

根据发布的命令行,这是 IntelliJ 用于运行您的项目的内容:

"C:\Program Files\Java\jdk-11.0.1\bin\java.exe" --module-path %PATH_TO_FX% --add-modules=javafx.controls,javafx.fxml

So the issue is quite clear: In the VM options you have set verbatimwhat the tutorialsays:

所以问题很清楚:在 VM 选项中,您已经逐字设置了教程所说的内容:

enter image description here

在此处输入图片说明

But you haven't applied your real pathfor PATH_TO_FX, as it is suggested in the picture inserted after that command in the tutorial:

可是你有没有应用你的真实路径PATH_TO_FX,因为它是在教程命令后插入的图片提示:

enter image description here

在此处输入图片说明

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:

IntelliJ 不解析该变量并且未设置模块路径,因此您会收到预期错误,表明未找到 JavaFX 模块:

Error occurred during initialization of boot layer
java.lang.module.FindException: Module javafx.controls not found

Solution

解决方案

This can be solved in two ways:

这可以通过两种方式解决:

  1. Apply your path:
  1. 应用您的路径:

Edit run configurations, and in the VM options add your path:

编辑运行配置,并在 VM 选项中添加您的路径:

--module-path "C:\Program Files\Java\javafx-sdk-11.0.1\lib" --add-modules=javafx.controls,javafx.fxml

Apply, and run. It should work.

申请,然后运行。它应该工作。

  1. Add the environment variable
  1. 添加环境变量

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 libfolder:

您还可以设置环境变量。转到IntelliJ->File->Settings->Appearance & Behavior->Path Variables,然后添加PATH_TO_FX,以及lib文件夹的路径:

enter image description here

在此处输入图片说明

And then you can use the literals $PATH_TO_FX$or ${PATH_TO_FX}in the VM options:

然后您可以使用文字$PATH_TO_FX$${PATH_TO_FX}在 VM 选项中:

--module-path ${PATH_TO_FX} --add-modules=javafx.controls,javafx.fxml

Apply, and run.

申请,然后运行。

Note that this is a more permanent solution that can be apply to any other JavaFX project.

请注意,这是一个更持久的解决方案,可以应用于任何其他 JavaFX 项目。

回答by M22

The top solution might be working with a simple case, where you did not specify VM options, but sometimes it might be more complicated.

最佳解决方案可能适用于一个简单的案例,您没有指定VM options,但有时可能会更复杂。

If the top solution is not your case then there is another thing to check: check whether your project root foldercontains .imlfile of your Modulename (check in project structure).

如果最佳解决方案不是您的情况,那么还有另一件事要检查:检查您的项目根文件夹是否包含模块名称的.iml文件(检查项目结构)。

If the .imlis missing then you need to recreate the Module in project structure. You need to recreate the module in project structure, so module will generate and link .iml file.

如果.iml丢失,那么您需要在项目结构中重新创建模块。您需要在项目结构中重新创建模块,因此模块将生成并链接 .iml 文件。