12

Currently in our application we have multiple main classes and executing them individually using below commands separately.

java -Xmx1024M -cp /path/to/jar/MyApp.jar com.....MyAppMain1

java -Xmx1024M -cp /path/to/jar/MyApp.jar com.....MyAppMain2

java -Xmx1024M -cp /path/to/jar/MyApp.jar com.....MyAppMain3

Now trying to use spring boot. What do we do to achieve the same?

In pom.xml have

……. <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> </parent> …….. 

using spring boot and executing the command

java -Xmx1024M -cp /path/to/jar/MyApp.jar com.....MyAppMain1

getting error as [ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project MyApp:The parameters 'mainClass' for goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java are missing or invalid

2
  • Did you try mvn clean install ? looks like error the packing, please try it and let us know.; Commented Sep 16, 2019 at 14:57
  • yes, tried it. Doing maven clean install didn't help. Looking close at the parent pom it is using exec-maven-plugin and is expecting the start class mainClass>${start-class}</mainClass>. I am not sure how to pass this start class for different programs we have. Commented Sep 16, 2019 at 15:08

3 Answers 3

14

Spring Boot gives several ways:

  • specify main class as system property:
java -cp app.jar -Dloader.main=com.company.MyAppMain1 org.springframework.boot.loader.PropertiesLauncher 
  • configure main class in Maven pom.xml <properties> section:
<properties> <start-class>com.company.MyAppMain1</start-class> </properties> 

Note that this property will only be evaluated if you use spring-boot-starter-parent as <parent> in your pom.xml.

  • configure main class for spring-boot-maven-plugin:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>${start-class}</mainClass> </configuration> </plugin> </plugins> </build> 

Note: plugin configuration can be performed in Maven profile, so by activating different profiles, you'll run app with different main class.

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

2 Comments

Thanks, for response. does the properties have to be provided as below without hardcoding the main right <properties> <start-class>${loader.main}</start-class> </properties> Also, how to run this program in eclipse? main class is passed as vm argument. Not sure how to run the program in eclipse
<properties> approach will work only if you are inheriting from spring-boot-starter-parent. If you do inherit from Spring's parent, then just set value of loader.main as parameter. There are multiple references how to run Maven on eclipse. I suggest to follow the instructions, e.g.: select pom.xml > Run As > clean verify > Add parameter etc.
6

Following are the different way to run Spring Boot main class:

1. Single main class in the Spring Boot Application

A. via <properties> section

<properties> <start-class>com.company.MyAppMain1</start-class> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> 

Note:

  1. When we use start-class property in properties section of pom.xml, we don't need to pass explicit value to plugin spring-boot-maven-plugin

B. via Custom property <custom-mainClass>

<properties> <custom-mainClass>com.company.MyAppMain1</custom-mainClass> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>${custom-mainClass}</mainClass> </configuration> </plugin> </plugins> </build> 

Note:

  1. Custom property custom-mainClass can be hard coded inside the property file as mentioned above 1.B

  2. Pass by maven property using build as mentioned below:

    mvn -U clean install -Dcustom-mainClass=com.company.MyAppMain1

C. via Hard coded value in plugin spring-boot-maven-plugin section

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.company.MyAppMain1</mainClass> </configuration> </plugin> </plugins> </build> 

2. Multiple main class in the Spring Boot Application

A. via org.springframework.boot.loader.PropertiesLauncher

Step 1:

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass> org.springframework.boot.loader.PropertiesLauncher </mainClass> </configuration> </plugin> </plugins> </build> 

Step 2: Use below command to start application

java -Dloader.main=com.company.MyAppMain1 -jar myapp.jar 

OR

java -cp myapp.jar -Dloader.main=com.company.MyAppMain1 org.springframework.boot.loader.PropertiesLauncher 

Note:

  1. Use -Dloader.main as Java VM arguments to pass dynamic main-class during start of application as shown in step 2
  2. org.springframework.boot.loader.PropertiesLauncher has moved to org.springframework.boot.loader.launch.PropertiesLauncher

B. via Custom property <custom-mainClass>

<properties> <custom-mainClass>com.company.MyAppMain1</custom-mainClass> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>${custom-mainClass}</mainClass> </configuration> </plugin> </plugins> </build> 

Note:

  1. Pass by maven property using build as mentioned below:

    mvn -U clean install -Dcustom-mainClass=com.company.MyAppMain1

This also a dynamic main-class but its good only during maven build. Not applicable during application startup as shown in step 2 of section 2.A

2 Comments

thank you. THIS IS GREAT. If i could give more updooties, I would.
Note that org.springframework.boot.loader.PropertiesLauncher has moved to org.springframework.boot.loader.launch.PropertiesLauncher
2

With Spring boot 3.2.x (Release Notes)

<properties> <start-class>org.springframework.boot.loader.launch.PropertiesLauncher</start-class> </properties> ... <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>${start-class}</mainClass> </configuration> </plugin> 

To run the jar with dynamic main class names

java -jar -Dloader.main=com.myapp.Main1 -jar myapp.jar java -jar -Dloader.main=com.myapp.Main2 -jar myapp.jar 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.