java11模块化开发,使用Java 11在Eclipse中进行混合模块化和非模块化开发

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.

解决方案

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值