简体   繁体   English

Java 模块和 JAR MANIFEST Class-Path 属性

[英]Java modules and the JAR MANIFEST Class-Path attribute

I have a Java 8 application I am working on converting to Java 11 and of course with this I am working with the module system for the first time.我有一个 Java 8 应用程序,我正在努力转换为 Java 11,当然这是我第一次使用模块系统。

I have my application Maven assembly configuration set such that all the JAR files are put in a /lib directory of the bin distribution.我有我的应用程序 Maven 程序集配置集,以便所有 JAR 文件都放在 bin 分发的 /lib 目录中。 The maven-jar-plugin then sets the MANIFEST Class-Path value with all the JAR file names.然后,maven-jar-plugin 使用所有 JAR 文件名设置 MANIFEST Class-Path 值。 The application is run with a simple java -jar /lib/application.jar该应用程序使用简单的java -jar /lib/application.jar运行

Moving to Java 11, if I continue to build the bin distribution this way, I'm curious to know how the runtime interprets the MANIFEST Class-Path value?转到 Java 11,如果我继续以这种方式构建 bin 分发,我很想知道运行时如何解释 MANIFEST Class-Path 值? I read through the JAR specs ( https://docs.oracle.com/en/java/javase/11/docs/specs/jar/jar.html ) and I think I understand it as: I read through the JAR specs ( https://docs.oracle.com/en/java/javase/11/docs/specs/jar/jar.html ) and I think I understand it as:

  1. Any modular JAR file (contains module-info.class) will be treated as a non-modular JAR.任何模块化 JAR 文件(包含 module-info.class)都将被视为非模块化 JAR。
  2. Any non-modular JAR file (no module-info.class) will be treated as a non-modular JAR.任何非模块化 JAR 文件(无 module-info.class)将被视为非模块化 JAR。
  3. Since everything is on the Class-Path, everything is essentially an unnamed module由于一切都在类路径上,一切本质上都是一个未命名的模块
  4. Since everything is on the Class-Path, the module system is essentially not being used at runtime由于一切都在 Class-Path 上,因此模块系统在运行时基本上没有被使用
  5. Since everything is on the Class-Path, the application should run essentially as if it's still running as Java 8?由于一切都在 Class-Path 上,因此应用程序基本上应该像仍然以 Java 8 一样运行?

If I did want to change this, would I start putting any modular JAR file on the --module-path and keep the other JAR files on the Class-Path?如果我确实想改变这一点,我会开始将任何模块化 JAR 文件放在 --module-path 上,并将其他 JAR 文件放在 Class-Path 上吗?

Exactly;确切地; Running via -jar all your code is in the unnamed module, while the JDK classes themselves are running on modules, that's why you can see "java.base/" in the stacktraces running with JDK 9+.通过 -jar 运行您的所有代码都在未命名的模块中,而 JDK 类本身在模块上运行,这就是为什么您可以在使用 JDK 9+ 运行的堆栈跟踪中看到“java.base/”的原因。

So your invocation would have to change from java -jar lib/application.jar to java --module-path lib --module application/your.main.class So your invocation would have to change from java -jar lib/application.jar to java --module-path lib --module application/your.main.class

You can improve it further by using JLink and/or JPackage, where you can create an executable that already executes your module, like ./bin/application .您可以使用 JLink 和/或 JPackage 进一步改进它,您可以在其中创建一个已经执行您的模块的可执行文件,例如./bin/application

Start by adding all jars to the module path and see if the Java doesn't say anything about split packages.首先将所有 jars 添加到模块路径中,然后查看 Java 是否没有说明拆分包。 So by doing that, it almost certain that your modules will be "automatic modules".因此,通过这样做,几乎可以肯定您的模块将是“自动模块”。 So you can experience some ClassNotFoundException.所以你会遇到一些ClassNotFoundException。

If that happens, you'll need to --add-module module .如果发生这种情况,您需要--add-module module

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM