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

opai
  • 253
  • 1
  • 5
  • 14
  • Did you try `mvn clean install` ? looks like error the packing, please try it and let us know.; – Jonathan JOhx Sep 16 '19 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}. I am not sure how to pass this start class for different programs we have. – opai Sep 16 '19 at 15:08

2 Answers2

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.

stacker
  • 68,052
  • 28
  • 140
  • 210
Oleksii Zghurskyi
  • 3,967
  • 1
  • 16
  • 17
  • Thanks, for response. does the properties have to be provided as below without hardcoding the main right ${loader.main} Also, how to run this program in eclipse? main class is passed as vm argument. Not sure how to run the program in eclipse – opai Sep 16 '19 at 17:49
  • `` 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](https://www.vogella.com/tutorials/EclipseMaven/article.html). I suggest to follow the instructions, e.g.: select pom.xml > Run As > clean verify > Add parameter etc. – Oleksii Zghurskyi Sep 16 '19 at 20:05
1

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

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

Punit Tiwari
  • 496
  • 4
  • 8