我创建了一个模块 com.company.ep
,它位于源文件夹 com.company.ep
中。 (是的,我已经从构建路径中删除了 src
并将其删除!)在源文件夹中,我有几个包如下:
com.company.ep <--- root source folder
com.company.ep.main <--- package 1
com.company.ep.model <--- package 2
com.company.ep.view <--- package 3
// ... more packages
module-info.java
主类位于包 com.company.ep.main.Main
中。在我的 module-info.java
中,我配置了依赖项:
module com.company.ep {
exports com.company.ep.main;
exports com.company.ep.model;
exports com.company.ep.view;
// ... more exports
requires javafx.controls;
requires javafx.graphics;
}
当我尝试启动我的程序时,eclipse 告诉我:
Error occurred during initialization of boot layer
java.lang.module.FindException: Module javafx.controls not found, required by com.company.ep
因此,我尝试在命令提示符下运行它:
java -p d:\Applications\openjfx-sdk-11\lib;bin -m com.company.ep/com.company.ep.main.Main
bin
是 eclipse 的输出文件夹,并且有效。
所以,我转到 Properties → Run/Debug Settings → Main → Show Command Line
,它显示:
D:\Applications\openjdk-11.0.1\bin\javaw.exe -Dfile.encoding=UTF-8 -p "D:\Development\Eclipse-Workspace\MyProject\bin"-classpath "D :\Applications\openjfx-sdk-11\lib\javafx.base.jar;D:\Applications\openjfx-sdk-11\lib\javafx.controls.jar;D:\Applications\openjfx-sdk-11\lib\javafx.fxml.jar;D:\Applications\openjfx-sdk-11\lib\javafx.graphics.jar;D:\Applications\openjfx-sdk-11\lib\javafx.media.jar;D:\Applications\openjfx -sdk-11\lib\javafx.swing.jar;D:\Applications\openjfx-sdk-11\lib\javafx.web.jar;D:\Applications\openjfx-sdk-11\lib\javafx-swt.jar "-m com.company.ep/com.company.ep.main.Main
我创建了一个添加了所有 JAR 的用户库,并将该库添加到项目的 Modulepath。
然后我尝试在 Run/Debug Settings
中的 VM arguments
中显式设置模块路径:-p D:\Applications\openjfx-sdk- 11\lib
,我还是没有运气。
我的问题是:
- 为什么是
javaw.exe
? - 为什么是
类路径
?因为我的库被添加为模块路径条目。 - 如何在eclipse中配置模块依赖。
我不确定我是否正确配置了 eclipse,或者它是否可能是 OpenJDK 的问题,因为当我在另一台安装了 Oracle Java SE 的计算机上工作时它可以正常工作。
谢谢!
最佳答案
可以在 OpenJFX docs for Eclipse 中找到有关 Eclipse 运行模块化项目失败原因的解释。 (来自 IDE 部分的模块化)。
如前所述:
Being a modular project, and since we already added the JavaFX SDK library to the module-path, there is no need to add any VM arguments.
但是如果你在 Eclipse 上运行你会得到提到的错误:
Error occurred during initialization of boot layer java.lang.module.FindException: Module javafx.graphics not found, required by hellofx
为什么会失败??
如文档中所述:
This exception happens because the Eclipse ant task overrides the module-path
这是怎么发生的??
检查应用的命令行(Show Command Line
from Run Configurations...),您可以找出原因:
$JAVA_HOME/bin/java -Dfile.encoding=UTF-8 \
-p bin/hellofx \
-classpath $PATH_TO_FX \
-m hellofx/org.openjfx.MainApp
如果您复制并粘贴它并在终端中运行它,它当然会失败并显示相同的消息。原因是 Eclipse 没有将 JavaFX 库添加到模块路径。
如果任务生成了错误的参数,让我们尝试通过编辑运行配置添加我们自己的 VM 参数来修复它...并添加 -p $PATH_TO_FX:bin/hellofx
。
但是如果你运行它,它又会失败。
让我们通过运行配置中的 Show Command Line
检查原因...
$JAVA_HOME/bin/java -Dfile.encoding=UTF-8 \
-p $PATH_TO_FX:bin/hellofx \
-p bin/hellofx \
-classpath $PATH_TO_FX \
-m hellofx/org.openjfx.MainApp
如您所见,用户的 VM 参数添加在默认的 ant 任务参数之前,因此有两个 -p
(--module-path
) 选项,并且第一个(带有 JavaFX jar 的用户的)被第二个(仅项目的模块)覆盖,因此,同样,JavaFX jar 没有添加到模块路径,因此你得到了错误。
那么我们该如何解决呢??
如链接文档中所述,可能的解决方法是:
To prevent this issue click on Run -> Run Configurations... -> Java Application -> Dependencies, select Override Dependencies... and add
-p /path-to/javafx-sdk-11/lib:bin/hellofx
, and press Override.
使用此解决方案,您可以看到它有效,您可以检查命令行:
$JAVA_HOME/bin/java -Dfile.encoding=UTF-8 \
-p $PATH_TO_FX:bin/hellofx \
-p bin/hellofx \
-classpath $PATH_TO_FX \
-p /path-to/javafx-sdk-11/lib:bin/hellofx \
-m hellofx/org.openjfx.MainApp
基本上我们再次添加“正确的”模块路径选项,在所有失败的选项之后。
虽然现在项目运行起来了,但是这个解决方案显然不太好。
Here您可以找到从 OpenJFX 文档中引用的示例。
编辑
根据@kleopatra 的评论,另一种解决方法如下:
出于某种原因,JavaFX11 库(包含模块化 jar)未被扫描,Eclipse 未将这些 jar 包含在其 -p
选项中,而是包含在类路径中:
$JAVA_HOME/bin/java -Dfile.encoding=UTF-8 \
-p bin/hellofx \
-classpath $PATH_TO_FX \
...
但是,如果您将这些 jar 直接添加到模块路径,它会添加它们,并且运行良好:
$JAVA_HOME/bin/java -Dfile.encoding=UTF-8 \
-p bin/hellofx:$PATH_TO_FX/javafx.base.jar:...:$PATH_TO_FX/javafx.controls \
...
有了这个,就不再需要重写依赖了。
编辑 2
正如@mipa 在评论中指出的那样,有一个 bug提交了这个问题,它已经解决了。我用 Eclipse 2018-12 M2 测试过它(4.10.0M2) Build id:20181108-1653,它仅适用于 JavaFX11
库(应该如此):
$JAVA_HOME/bin/java -Dfile.encoding=UTF-8 \
-p bin/hellofx:$PATH_TO_FX/javafx.base.jar:... \
-m hellofx/org.openjfx.MainApp
关于java - 即使显式提供了模块路径,Eclipse 也找不到模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53295226/