尝试执行模块化 java jar 文件时出错

error trying to execute a modular java jar file


我有一个 Java 11 模块化程序,在命令提示符下 运行 没问题。但是,当我将它打包到 Jar 文件(Jar 11)中并再次尝试 运行 时,我收到“*错误:无法在模块 [ 中找到或加载 main class farm.vet.VetMain =97=]*".

我附上了一张图片,显示了我从命令行执行的各种命令。第一行正常显示程序运行ning(没有使用jar)。涉及几个模块,farm.vet 是起点,farm.vet.VetMain 包含 `main()`。

我创建了 Jar 文件 - 它包括各种模块的模块信息文件。但是,当我之后尝试 运行 它时 - 那是我收到错误的时候。然后我描述了 Jar 文件并关注 "No module descriptor"。我相信我应该在 Modular Jar 文件的顶部有一些东西 - 这是我在 运行ning the Jar 中出错的原因吗?

我还包含了我的目录结构('out' 是我所有 class 文件的父目录)。

非常感谢任何帮助,
肖恩。

java -p out -m farm.vet/farm.vet.VetMain

jar -cvf mods/farm.vet.jar -C out/ .
added manifest
adding: farm.animals/(in = 0) (out= 0)(stored 0%)
adding: farm.animals/farm/(in = 0) (out= 0)(stored 0%)
adding: farm.animals/farm/Animal.class(in = 360) (out= 246)(deflated 31%)
adding: farm.animals/farm/cattle/(in = 0) (out= 0)(stored 0%)
adding: farm.animals/farm/cattle/Cattle.class(in = 829) (out= 475)(deflated 42%)
adding: farm.animals/farm/cattle/Cow.class(in = 999) (out= 548)(deflated 45%)
adding: farm.animals/module-info.class(in = 195) (out= 149)(deflated 23%)
adding: farm.owner/(in = 0) (out= 0)(stored 0%)
adding: farm.owner/farm/(in = 0) (out= 0)(stored 0%)
adding: farm.owner/farm/owner/(in = 0) (out= 0)(stored 0%)
adding: farm.owner/farm/owner/Owner.class(in = 1569) (out= 853)(deflated 45%)
adding: farm.owner/module-info.class(in = 200) (out= 148)(deflated 26%)
adding: farm.vet/(in = 0) (out= 0)(stored 0%)
adding: farm.vet/farm/(in = 0) (out= 0)(stored 0%)
adding: farm.vet/farm/vet/(in = 0) (out= 0)(stored 0%)
adding: farm.vet/farm/vet/VetMain.class(in = 710) (out= 448)(deflated 36%)
adding: farm.vet/module-info.class(in = 194) (out= 143)(deflated 26%)

java -p mods -m farm.vet/farm.vet.VetMain
Error: Could not find or load main class farm.vet.VetMain in module farm.vet

jar --describe-module --file mods/farm.vet.jar
No module descriptor found. Derived automatic module.
farm.vet automatic
requires java.base mandated
contains farm.animals
contains farm.animals.farm
contains farm.animals.farm.cattle
contains farm.owner
contains farm.owner.farm.owner
contains farm.vet
contains farm.vet.farm.vet


  • 将此参数添加到 jar 创建命令:--main-class farm.vet.VetMain

  • 运行 jar 文件使用命令:java -p mods -m farm.vet

每个 JAR 文件只能有一个模块,因为 module-info.class 必须位于 JAR 文件的根目录中。

尝试:

jar -cvf mods/farm.vet.jar -C out/farm.vet/ .
jar -cvf mods/farm.owner.jar -C out/farm.owner/ .
jar -cvf mods/farm.animals.jar -C out/farm.animals/ .

基于Project Jigsaw: Module System Quick-Start Guide

A modular JAR is a regular JAR file that has a module-info.class in its top-level directory.

The jar Command description

An archive becomes a modular JAR when you include a module descriptor, module-info.class, in the root of the given directories or in the root of the .jar archive.

上面提到的“给定目录的根”让我有点困惑