Java 9:模块路径上可能有2个具有相同名称的模块 [英] Java 9: Possible to have 2 modules with same name on module path

查看:463
本文介绍了Java 9:模块路径上可能有2个具有相同名称的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模块路径上是否有2个模块具有完全相同的名称(但内容略有不同)?

Is it possible to have 2 modules with the exact same name (but with slightly different contents) on the module path?

据我所知, Java 9编译器不会抱怨它。我有2个模块声明如下:

As far as I can tell, the Java 9 compiler does not complain about it. I have 2 modules declared as follows:

module com.dj.helper {
    exports com.dj.helper;
}

两者都包含 com.dj.helper 包但在包中内容不同。然后在我的主应用程序中,我想要导入这个模块:

Both contain the com.dj.helper package but within the package the contents are different. Then in my main application, I am looking to import this module:

module com.dj {
    requires com.dj.helper;
}

两个名称相同的模块都在我的模块路径上。

Both modules with the same name are on my module path.

我希望在编译我的 com.dj 模块时,编译器会抱怨同一模块存在两次,但它没有。这是否有效意味着你的模块路径上可能有2个版本的同一个jar,而Java不知道要使用哪个版本?

I was hoping that when compiling my com.dj module that the compiler would complain about the same module existing twice but it does not. Does this effectively mean you could have 2 versions of the same jar on your module path and Java will not know which one to use?

推荐答案

模块系统的 JEP 261 描述了模块路径如下:

JEP 261 of the module system describes the module path as follows:


模块路径是一个序列,其中每个元素都是模块定义或包含模块定义的目录。每个模块定义是

A module path is a sequence, each element of which is either a module definition or a directory containing module definitions. Each module definition is either


  • 模块工件,即模块化JAR文件或JMOD包含已编译模块定义的文件,或者

  • A module artifact, i.e., a modular JAR file or a JMOD file containing a compiled module definition, or else

一个爆炸模块目录,按照惯例,其名称是模块的名称和其内容是与包层次结构对应的爆炸目录树。

An exploded-module directory whose name is, by convention, the module's name and whose content is an "exploded" directory tree corresponding to a package hierarchy.

然后描述了模块解析机制:

It then describes the module resolution mechanism:


在模块路径中搜索特定名称的模块时,模块系统采用第一个定义该名称的模块。版本字符串(如果存在)将被忽略; 如果模块路径的元素包含具有相同名称的多个模块的定义,则解析失败,编译器,链接器或虚拟机将报告错误并退出。构建工具和容器应用程序配置模块路径以避免版本冲突;模块系统的目标不是解决版本选择问题。

When searching a module path for a module of a particular name, the module system takes the first definition of a module of that name. Version strings, if present, are ignored; if an element of a module path contains definitions of multiple modules with the same name then resolution fails and the compiler, linker, or virtual machine will report an error and exit. It is the responsibility of build tools and container applications to configure module paths so as to avoid version conflicts; it is not a goal of the module system to address the version-selection problem.

如上所述,这意味着编译器会抱怨仅当同一目录中存在的两个具有相同名称的模块

As explained, it means that the compiler will complain only if two modules with the same name exist in the same directory.

这篇关于Java 9:模块路径上可能有2个具有相同名称的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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