有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

即使显式提供了模块路径,java Eclipse也无法找到模块

我已经创建了一个模块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中。在mymodule-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的用户库,该库被添加到项目的模块路径中

然后我尝试在VM arguments中显式地设置模块路径在Run/Debug Settings-p D:\Applications\openjfx-sdk-11\lib中,我仍然没有运气

我的问题是:

  • 为什么javaw.exe
  • 为什么classpath?因为我的库是作为模块路径条目添加的
  • 如何在eclipse中配置模块依赖项

我不确定我是否正确配置了eclipse,也不确定这是否可能是OpenJDK的问题,因为我在另一台安装了Oracle Java SE的计算机上工作时,OpenJDK出现了问题

谢谢大家!


共 (1) 个答案

  1. # 1 楼答案

    关于Eclipse在运行模块化项目时失败的原因,可以在OpenJFX docs for 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来自运行配置…),你可以找出原因:

    $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 \
        ...
    

    eclipse module-path

    这样就不需要重写依赖项了

    编辑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 
    

    Eclipse 4.10