问题
It's been a while since I do Java programming and I am surprised to come back to it with the entire landscape being foreign to me post project jig-saw.
I have trouble using Eclipse (2018-09, 4.9.0) standard Java project with mixed modular and non-modular environment. Specifically, I am attempting to combine JavaFX 11 (modularized) and Apache POI 4.1 (non-modularized) using Eclipse platform (base Java project without Gradle or Maven).
In my module-info.java I have the following,
module myapp {
requires javafx.base;
requires javafx.graphics;
requires javafx.fxml;
requires javafx.controls;
requires javafx.web;
exports myapp.gui;
opens myapp.gui to javafx.fxml;
}
I find that wherever I have the Apache POI code I get the following error in Eclipse
The import cannot be resolved
Adding the following in the module-info.java using the automatic module created for Apache POI like so,
requires poi;
Produces a warning in Eclipse indicating automatic module is not stable which seems to be recognized but continue to produce the cannot be resolved error.
I have also tried putting the main POI jar file in class path as oppose to module-path with no avail.
The code involving Apache POI in separation from the UI works. I simply have to remove the use of module-info.java which I assume puts the project in some sort of legacy development mode sans modularization?
Can someone give me a pointer as to what I am doing wrong and guide me to setup a project with mixed modularized and non-modularized libraries?
Thanks in advance.
回答1:
Your code is encapsulated in a module if you have a module-info.java
file in the default package. To use something of a JAR inside a module,
- the JAR must be on the Modulepath and
- the module must be explicitly declared as required in the
module-info.java
:requires <module_name>;
.
Otherwise, import statements can not be resolved. Nothing of the items on the Classpath is accessible from a module; and nothing on the Modulepath without a requires ...
in module-info.java
can be used in a module. Therefore, in your case, it wouldn't work without requires poi;
(and without the POI library on the Modulepath).
The "Name of automatic module '...' is unstable" warning is only a hint, that the POI JAR has not built as a modul, but used as a module. Actually this means that if you rename the JAR file, the module name can also change and you have to adapt the module-info.java
file. Technically, the POI JAR contains neither a module-info.class
file nor a META-INF/MANIFEST.MF
file with the line Automatic-Module-Name: ...
. So the module name poi
is derived from the file name of the JAR. The "Name of automatic module '...' is unstable" warning can be turned off in Project > Properties: Java Compiler > Errors/Warnings in the Module section.
To sum it up, you do not have mixed modular and non-modular dependencies. Instead, legacy JARs automatically become modules by deriving the module name from the file name. In this case, Eclipse warns you by default that the application can be broken by simply changing the file name of that JAR.
回答2:
So, as it turned out. I did goofed. There are 5 inter dependent jar file from the POI library which has caused the error "The import cannot be resolved"
Once I included all 5 files in the module-info.java the project compiled.
requires poi.excelant;
requires poi.ooxml;
requires poi.ooxml.schemas;
requires poi.scratchpad;
requires poi;
I am sure not all of them is necessary, but I included all of them just in case anyways.
Thanks everyone for reading and offering affirmation that I have not done anything to break Eclipse.
来源:https://stackoverflow.com/questions/55638253/mixed-modular-and-non-modular-development-in-eclipse-using-java-11