问题描述
距我进行Java编程已经有一段时间了,我很惊讶地回到项目现场,整个项目对我来说都是陌生的.
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.
我在混合模块化和非模块化环境中使用Eclipse(2018-09,4.9.0)标准Java项目时遇到麻烦.具体来说,我正在尝试使用Eclipse平台(不带Gradle或Maven的基础Java项目)将JavaFX 11(模块化)和Apache POI 4.1(非模块化)组合在一起.
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).
在我的module-info.java中,我有以下内容,
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;
}
我发现无论我在哪里拥有Apache POI代码,我在Eclipse中都会收到以下错误消息
I find that wherever I have the Apache POI code I get the following error in Eclipse
The import cannot be resolved
使用为Apache POI创建的自动模块,在module-info.java中添加以下内容,
Adding the following in the module-info.java using the automatic module created for Apache POI like so,
requires poi;
在Eclipse中产生警告,指示自动模块不稳定,这似乎可以识别,但会继续产生无法解决的错误.
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.
我也尝试将主POI jar文件放在类路径中,与模块路径相对,但无济于事.
I have also tried putting the main POI jar file in class path as oppose to module-path with no avail.
涉及与用户界面分离的Apache POI的代码.我只需要删除对module-info.java的使用,我认为该模块会使项目处于某种传统的开发模式,而无需模块化?
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?
谢谢.
推荐答案
如果默认程序包中有module-info.java
文件,则您的代码将封装在模块中. 要在模块内使用JAR功能,
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,
- JAR必须位于 Modulepath 和 上
- 必须在
module-info.java
:requires <module_name>;
中明确声明模块为必需的.
否则,无法解析导入语句. Classpath 上的所有项目都无法从模块访问;并且 Modulepath 上没有在module-info.java
中没有requires ...
的任何内容都不能在模块中使用.因此,根据您的情况,如果没有requires poi;
(并且没有 Modulepath 上的POI库),它将无法正常工作.
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).
自动模块'...'的名称不稳定" 警告只是一个提示,因为POI JAR尚未构建为模块,但已使用作为一个模块.实际上,这意味着,如果重命名JAR文件,则模块名称也可以更改,并且必须修改module-info.java
文件.从技术上讲,POI JAR既不包含module-info.class
文件,也不包含带有Automatic-Module-Name: ...
行的META-INF/MANIFEST.MF
文件.因此,模块名称poi
是从JAR的文件名派生的.可以在模块中的项目>属性:Java编译器>错误/警告中关闭自动模块'...'的名称不稳定" 警告. 部分.
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.
总结起来,您没有混合的模块化和非模块化依赖性.相反,通过从文件名中导出模块名称,旧版JAR会自动成为模块.在这种情况下,Eclipse默认会警告您,只需更改该JAR的文件名即可破坏应用程序.
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.
这篇关于使用Java 11在Eclipse中进行混合模块化和非模块化开发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!