使用 Java 11 在 Eclipse 中混合模块化和非模块化开发 [英] Mixed Modular and Non-Modular Development in Eclipse using Java 11

查看:38
本文介绍了使用 Java 11 在 Eclipse 中混合模块化和非模块化开发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有一段时间没有进行 Java 编程了,我很惊讶地重新回到 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.

涉及与 UI 分离的 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 必须位于模块路径
  • 模块必须在module-info.java中明确声明为:requires ;.

否则无法解析import语句.Classpath 上的任何项目都不能从模块访问;在 module-info.java 中没有 requires ...Modulepath 上的任何东西都不能在模块中使用.因此,在您的情况下,如果没有 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 中混合模块化和非模块化开发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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