Java 9 中的可靠配置

Reliable configuration in Java 9

Java9 中 Jigsaw 项目的主要目标之一是可靠的配置。也就是说,Java 9 承诺解决 class 路径机制缺陷,该缺陷允许 java 启动程序 运行确保所有必需的 classes 都可以在 运行 时间内加载,而这通常会导致 java.lang.NoClassDefFoundErrors。

这是通过在 module-info.java 中声明模块依赖关系和全新的 --module-path 选项来完成的。在启动 Java 应用程序之前分析模块图。

但是,我仍然可以做到以下几点。

  1. 假设我有两个模块 com.spacey.explorercom.spacey.rocketcom.spacey.explorer 使用由 com.spacey.rocket 模块定义和导出的 class com.spacey.rocket.RocketZ。在编译和 JARing 两个模块后,一切 运行s 都正确。
  2. 现在我从 com.spacey.rocket 模块中删除了 com.spacey.rocket.RocketZ 类型,并仅重新编译和重新 JAR 这一个模块。
  3. 我运行以前编译的com.spacey.explorer模块和新编译的com.spacey.rocket模块。
  4. 我得到... java.lang.NoClassDefFoundError,这在应用程序正常运行大约 4 小时后是可能的。

有没有办法真正确保当运行宁Java应用程序不仅验证模块图(模块路径)的完整性而且还全面检查了实际类型的可访问性吗?

Is there a way to really make sure that when running Java application not only the module graph (module path) completeness is verified but also a comprehensive check of the actual type accessibility is done?

不在 JVM 中,不。模块系统在工件级别上运行,如果一个工件声称是正确的模块 is present,它会很高兴。除此之外,不会执行进一步的检查。

也就是说,JDeps 应该可以帮助您。它分析您的项目及其依赖项,并在个人 类 级别上运行。会指出找不到的依赖。

在你的例子中:

$ jdeps -R --module-path jars-dir -m com.spacey.explorer

> com.spacey.explorer    -> com.spacey.rocket    not found

我认为其他答案在技术方面有所帮助,但除此之外,我认为您的 可靠 部分有点错误。

This 谈论 脆弱 类路径:

All of these are the results of a brittle classpath. What that means is that ClassLoaders don’t have a good mechanism for distinguishing one loaded class from another class of the same name, or for isolating classes loaded by one ClassLoader from classes loaded by another.

以及模块为此提供的答案:

Java Modules will provide reliable configuration and strong encapsulation. If you have incompatibilities, you will discover these at build time instead of some indeterminate time after your application has started running in production.

换句话说:我认为你认为模块的设计是为了防止所有LinkageError(所有No之母..FoundError 东西)。模块使您可以更轻松地 构建 东西。您找到了一个很好的例子,这不一定转化为关于 运行时 问题的完美安全网。

换句话说:您对 "reliable modules" 的解释与 Jigsaw 项目实际试图提供的 "reliable modules" 的承诺不同步。