Eclipse cannot find module even the module path is explicitly provided

懵懂的女人 提交于 2021-02-07 06:13:01

问题


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

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;
}

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 is the output folder of eclipse, and it worked.

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

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

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.

My questions are:

  • Why javaw.exe?
  • Why classpath? As my library is added as a module-path entry.
  • How to configure the module dependencies in eclipse.

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.

Thank you!


回答1:


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

As it was already mentioned:

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.

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

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

So why is it failing??

As explained in the docs:

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

How does this happen??

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 

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.

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.

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 

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:

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.

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.

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

EDIT

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

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.

EDIT 2

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 



来源:https://stackoverflow.com/questions/53295226/eclipse-cannot-find-module-even-the-module-path-is-explicitly-provided

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!