Eclipse找不到模块,即使已明确提供模块路径 [英] Eclipse cannot find module even the module path is explicitly provided

查看:496
本文介绍了Eclipse找不到模块,即使已明确提供模块路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个位于源文件夹com.company.ep中的模块com.company.ep. (是的,我已经从构建路径中删除了src并删除了它!)在源文件夹中,我有几个软件包,如下所示:

I have created a module com.company.ep that is located in the source folder com.company.ep. (Yes, I have removed src from the build path and deleted it!) Inside the source folder, I have a couple of packages as the following:

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中,我已经配置了依赖项:

The main class is located in the package com.company.ep.main.Main. In my module-info.java, I have configured the dependencies:

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告诉我:

When I tried to launch my program, eclipse told me that:

Error occurred during initialization of boot layer
java.lang.module.FindException: Module javafx.controls not found, required by com.company.ep

因此,我尝试在命令提示符下运行它:

So, I tried to run it on the command prompt:

java -p d:\Applications\openjfx-sdk-11\lib;bin -m com.company.ep/com.company.ep.main.Main

bin是eclipse的输出文件夹,有效.

bin is the output folder of eclipse, and it worked.

所以,我去了Properties → Run/Debug Settings → Main → Show Command Line,它显示了:

So, I went to Properties → Run/Debug Settings → Main → Show Command Line, it showed:

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 .

I have created a user library with all JARs added, and the library is added to the project's Modulepath.

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

Then I have tried to set the module path explicitly in VM arguments in Run/Debug Settings: -p D:\Applications\openjfx-sdk-11\lib, I'd still no luck.

我的问题是:

  • 为什么javaw.exe?
  • 为什么classpath?由于我的库被添加为模块路径条目.
  • 如何在eclipse中配置模块依赖性.
  • Why javaw.exe?
  • Why classpath? As my library is added as a module-path entry.
  • How to configure the module dependencies in eclipse.

我不确定是否已正确配置了eclipse,或者在安装了Oracle Java SE的另一台计算机上工作时,它是否可能是OpenJDK的问题.

I am not sure if I have configured eclipse correctly, or whether it is probably a problem of OpenJDK as it worked when I worked on another computer with Oracle Java SE installed.

谢谢!

推荐答案

The explanation of why Eclipse fails on running your modular project can be found in the OpenJFX docs for Eclipse (modular from IDE section).

如前所述:

作为一个模块化项目,由于我们已经将JavaFX SDK库添加到了模块路径,因此无需添加任何VM参数.

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上运行,则会出现上述错误:

But if you run on Eclipse you will get the mentioned error:

启动层初始化期间发生错误 java.lang.module.FindException:hellofx必需的模块javafx.graphics找不到

Error occurred during initialization of boot layer java.lang.module.FindException: Module javafx.graphics not found, required by hellofx

那为什么会失败?

如文档中所述:

发生此异常是因为Eclipse ant任务覆盖了模块路径

This exception happens because the Eclipse ant task overrides the module-path

这是怎么发生的?

检查应用的命令行(来自运行配置"中的Show Command Line ...),您可以找到原因:

Checking the command line applied (Show Command Line from Run Configurations...), you can find out why:

$JAVA_HOME/bin/java -Dfile.encoding=UTF-8 \
    -p bin/hellofx \
    -classpath $PATH_TO_FX \
    -m hellofx/org.openjfx.MainApp 

如果您将其复制并粘贴并在终端中运行,则它当然会失败,并显示相同的消息.原因是Eclipse并未将JavaFX库添加到模块路径.

If you copy it and paste it and run it in a terminal, it will fail of course with the same message. The reason is that Eclipse doesn't add the JavaFX library to the module path.

如果任务生成了错误的参数,让我们尝试通过编辑运行配置...并添加-p $PATH_TO_FX:bin/hellofx来添加我们自己的VM参数来修复该问题.

If the task generates the wrong arguments, let's try to fix it by adding our own VM arguments by editing Run configurations... and adding -p $PATH_TO_FX:bin/hellofx.

但是,如果您运行它,它将再次失败.

But if you run it, it will fail again.

让我们用运行配置中的Show Command Line检查原因.

Let's check why, with Show Command Line from Run Configurations...

$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 jars)被第二个(仅用于项目的模块)覆盖,因此,同样,JavaFX jars未添加到模块路径中,因此会出现错误.

As you can see, the user's VM arguments are added before the default ant task arguments, so there are two -p (--module-path) options, and the first one (the user's one with the JavaFX jars) is overridden by the second one (only the project's module), so, again, the JavaFX jars are not added to the module path, and hence you get the error.

那么我们该如何解决呢?

So how can we fix it??

如链接文档中所述,可能的解决方法是:

As mentioned in the linked documentation, the possible fix is:

要防止出现此问题,请单击运行->运行配置...-> Java应用程序->依赖关系,选择覆盖依赖关系...并添加-p /path-to/javafx-sdk-11/lib:bin/hellofx,然后按覆盖.

使用此解决方案,您可以看到它有效,并且可以检查命令行:

With this solution, you can see it works, and you can check the command line:

$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 

基本上,我们在所有失败的选项之后 之后再次添加正确的"模块路径选项.

Basically we are adding again the "right" module path option, after all the failed ones.

现在该项目在运行,解决方案显然不是很好.

While now the project runs, the solution is obviously not nice.

在这里,您可以找到引用的示例来自OpenJFX文档.

Here you can find a sample referred from the OpenJFX documentation.

编辑

基于@kleopatra的注释,另一个可行的解决方法如下:

Based on @kleopatra comments, another workaround to make it work is the following:

由于某种原因,不扫描库JavaFX11(包含模块化jar),并且Eclipse不在其-p选项中,而是在类路径中包括了这些jar:

For some reason, the library JavaFX11 (that contains modular jars) is not scanned and Eclipse doesn't include those jars into its -p option, but into the classpath:

$JAVA_HOME/bin/java -Dfile.encoding=UTF-8 \
    -p bin/hellofx \
    -classpath $PATH_TO_FX \
    ...

但是,如果您直接将这些罐子添加到模块路径,它将添加它们,这样就可以正常运行:

But, if you add those jars directly to the module path, it will do add them, and this will run fine:

$JAVA_HOME/bin/java -Dfile.encoding=UTF-8 \
    -p bin/hellofx:$PATH_TO_FX/javafx.base.jar:...:$PATH_TO_FX/javafx.controls \
    ...

然后,不再需要覆盖依赖项.

Then with this there is no more need to override the dependencies.

编辑2

@mipa在评论中指出,存在一个错误就此问题提起诉讼,并且已经解决.我已经使用Eclipse 2018-12 M2 ( 4.10.0M2)内部版本号:20181108-1653,它仅与JavaFX11库一起使用(应该如此):

As @mipa points out in a comment, there was a 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, and it works with the JavaFX11 library only (as it should):

$JAVA_HOME/bin/java -Dfile.encoding=UTF-8 \
    -p bin/hellofx:$PATH_TO_FX/javafx.base.jar:... \
    -m hellofx/org.openjfx.MainApp 

这篇关于Eclipse找不到模块,即使已明确提供模块路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆