1

I try to learn about the modules introduced in java 9. I try to create a runnable Jar. I have the main module, which I want to run and one dependendency module, helloWorldModule.

Module: mainModule main/Main.java

public class Main {

    public static void main(String[] args) {
        Greeter greeter = new Greeter();
        greeter.sayHello();
    }
}


module mainModule {
    requires helloWorldModule;
}

Module: helloWorldModule helloWorld/Greeter.java

public class Greeter {
    
    public void sayHello(){
        System.out.println("Hello there!");
    }
}

module helloWorldModule {
    exports helloWorld;
}

I've exported the modules to two jars. I can run it like that without any problems.

   java --module-path lib --module mainModule/main.Main

But I have to have both modules inside my custom lib folder to make that work. On my research about that, I found here, that one Module is always compiled into one jar. This means, that I can not put my helloWorldModule inside the main module jar file, but as the mainModule requires that helloWorldModule, I can't create a runnable JAR file.

What is the correct way to package my program into a runnable file now?

M. Lee
  • 99
  • 3
  • 7
  • 2
    Create the module jar with `jar`’s `e` option to specify the main class. Then, you only need `--module mainModule` instead of `--module mainModule/main.Main` (or even use `-m mainModule`). Then, you can use `jlink`, to create a Java runtime containing (only the required) JDK modules and your modules. The resulting environment does already have an implied module path, so you can run the application with `java -m mainModule` when using the launcher of that environment. – Holger Sep 01 '22 at 17:34

0 Answers0