4

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

1
  • 3
    Note -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 --module is -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-class option was specified). Commented May 30, 2023 at 9:05

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.