且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-06-12 22:42:08

如果默认程序包中有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.