同一个jar文件中的两个相同的类

Two identical classes in the same jar file

我有一个 jar 文件 - 它恰好是使用 JAXB 从 XML 模式生成的。

我刚刚注意到在那个 jar 内的相同路径中,我有 2 个 类 具有完全相同的名称。事实上,如果我使用 7-zip 解压缩 jar,Windows 会询问我是否要将文件 "blah.class" 替换为 "blah.class"。

这怎么可能?从 Java 的角度和文件系统的角度来看? zip 格式是否允许这样做?我试过 google 但无济于事。

谢谢, 大卫.

是的,zip 允许 2 个文件具有相同的完全限定名称。

ant jar 参考页面指出:

Please note that the zip format allows multiple files of the same fully-qualified name to exist within a single archive. This has been documented as causing various problems for unsuspecting users. If you wish to avoid this behavior you must set the duplicate attribute to a value other than its default, "add".

Java 然后会从类路径中选择第一个。

我在同一个构建中包含相同接口的多个实现时遇到过类似的问题。如果您发现有必要这样做,并且正在使用 Maven,则可以使用排除标记从生成的 jar 文件中删除其中一个实现。