I'm trying to create a simple Spring Boot application and run it with Java Modules on the latest Java version.
I have created a Main class:
package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Main {
public static void main(final String[] args) {
System.out.println("Module: " + Main.class.getModule());
SpringApplication.run(Main.class, args);
}
}
And a module-info.java:
module spring.jpms {
requires spring.core;
requires spring.boot;
requires spring.boot.autoconfigure;
requires spring.context;
requires spring.beans;
opens org.example to
spring.core,
spring.beans,
spring.context;
}
When I run the Main class using an IDE generated command with module path, it works fine, the name of my application module printed out.
Module: module spring.jpms
...
When I run my application using mvn spring-boot:run or java -jar <target>.jar, the application is executed in the class-path mode. The module name is "unnamed":
...
Module: unnamed module @7923f745
...
I would like to know if there is a way to run a .jar file built by the Spring Boot Maven plugin with Java Modules, that is so the .jar files are loaded via module-path
See the complete example here: https://github.com/agavrilov76/spring-jpms
-jar <file>will always put the JAR file on the class-path. If it's a modular JAR file, and Spring can handle being on the module-path, then use--module <main-module>/<main-class>. The short name for--moduleis-m. The/<main-class>can be omitted if the module has been configured with a main class (e.g., when creating the JAR file, the--main-classoption was specified).