即使显式提供了模块路径,Eclipse 也找不到模块
Eclipse cannot find module even the module path is explicitly provided
我创建了一个位于源文件夹 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
主要 class 位于包 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。
然后我尝试在 VM arguments
中显式设置模块路径 Run/Debug Settings
: -p D:\Applications\openjfx-sdk-11\lib
,我仍然没有运气。
我的问题是:
- 为什么
javaw.exe
?
- 为什么
classpath
?因为我的库被添加为模块路径条目。
- 如何在eclipse中配置模块依赖。
我不确定我是否正确配置了 eclipse,或者它是否可能是 OpenJDK 的问题,因为当我在另一台安装了 Oracle Java SE 的计算机上工作时它工作正常。
谢谢!
可以在 OpenJFX docs for Eclipse(来自 IDE 部分的模块)中找到 Eclipse 为什么在 运行 宁您的模块化项目时失败的解释。
如前所述:
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
来自 运行 配置...),您可以找出原因:
$JAVA_HOME/bin/java -Dfile.encoding=UTF-8 \
-p bin/hellofx \
-classpath $PATH_TO_FX \
-m hellofx/org.openjfx.MainApp
如果您将其复制并粘贴并 运行 在终端中,它当然会失败并显示相同的消息。原因是 Eclipse 没有将 JavaFX 库添加到模块路径。
如果任务生成了错误的参数,让我们尝试通过编辑 运行 配置...并添加 -p $PATH_TO_FX:bin/hellofx
添加我们自己的 VM 参数来修复它。
但是如果你运行它,它会再次失败。
让我们检查一下原因,使用 运行 配置中的 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
如您所见,在默认的 ant 任务参数 之前 添加了用户的 VM 参数,因此有两个 -p
(--module-path
) 选项
那么我们该如何解决呢?
如链接文档中所述,可能的解决方法是:
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
基本上我们再次添加 "right" 模块路径选项,在 所有失败的选项之后。
虽然现在项目运行s,解决方案显然不好。
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 filed on this issue, and it has already been solved. I've tested it with 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
我创建了一个位于源文件夹 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
主要 class 位于包 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。
然后我尝试在 VM arguments
中显式设置模块路径 Run/Debug Settings
: -p D:\Applications\openjfx-sdk-11\lib
,我仍然没有运气。
我的问题是:
- 为什么
javaw.exe
? - 为什么
classpath
?因为我的库被添加为模块路径条目。 - 如何在eclipse中配置模块依赖。
我不确定我是否正确配置了 eclipse,或者它是否可能是 OpenJDK 的问题,因为当我在另一台安装了 Oracle Java SE 的计算机上工作时它工作正常。
谢谢!
可以在 OpenJFX docs for Eclipse(来自 IDE 部分的模块)中找到 Eclipse 为什么在 运行 宁您的模块化项目时失败的解释。
如前所述:
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
来自 运行 配置...),您可以找出原因:
$JAVA_HOME/bin/java -Dfile.encoding=UTF-8 \
-p bin/hellofx \
-classpath $PATH_TO_FX \
-m hellofx/org.openjfx.MainApp
如果您将其复制并粘贴并 运行 在终端中,它当然会失败并显示相同的消息。原因是 Eclipse 没有将 JavaFX 库添加到模块路径。
如果任务生成了错误的参数,让我们尝试通过编辑 运行 配置...并添加 -p $PATH_TO_FX:bin/hellofx
添加我们自己的 VM 参数来修复它。
但是如果你运行它,它会再次失败。
让我们检查一下原因,使用 运行 配置中的 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
如您所见,在默认的 ant 任务参数 之前 添加了用户的 VM 参数,因此有两个 -p
(--module-path
) 选项
那么我们该如何解决呢?
如链接文档中所述,可能的解决方法是:
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
基本上我们再次添加 "right" 模块路径选项,在 所有失败的选项之后。
虽然现在项目运行s,解决方案显然不好。
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 filed on this issue, and it has already been solved. I've tested it with 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