Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Spring Boot .jar with Java Modules?

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

like image 599
Alexey Gavrilov Avatar asked Sep 15 '25 21:09

Alexey Gavrilov


1 Answers

Just as an update, I can confirm that it is now possible with spring-boot-starter-parent:3.3.0 to execute the application with only a command such as:

mvn spring-boot:run

Alternatively, one can at the same time create an executable using:

 mvn clean package

and execute as(sample) :

java -jar target/jpms-spring-0.0.1-SNAPSHOT.jar 

Here is a reference: https://github.com/namannigam/jpms-spring

like image 66
Naman Avatar answered Sep 17 '25 12:09

Naman