Search code examples
javamodulejar

error trying to execute a modular java jar file



I have a Java 11 Modular program that runs fine from the Command prompt. However, when I package it in a Jar file (Jar 11) and try to run it again I am getting "*Error: Could not find or load main class farm.vet.VetMain in module farm.vet*".

I have enclosed an image showing the various commands I have executed from the command line. The first line shows the program running normally (no jar used). There are a few modules involved, farm.vet is the starting point with farm.vet.VetMain containing `main()`.

I create the Jar file - it includes the module-info files for the various modules. However when I try to run it afterwards - that's when I get the error. I then describe the Jar file and am concerned about "No module descriptor". I believe I should have something at the top of the Modular Jar file - is that the reason for my error in running the Jar?

I have also included my directory structure ('out' is the parent directory for all my class files).

Any help greatly appreciated,
Seán.

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



Solution

  • There can only be one module per JAR file, since module-info.class must be in the root of the JAR file.

    Try:

    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/ .
    

    Based on 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.

    and 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.

    I got a bit confused by "root of the given diretories" being mentioned above