Unable to find suitable main class, add property "mainClass", Spring boot

Greetings, I am developing a spring-based project and I created maven submodules, but when I try to run my project, it gives an error

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.2.5.RELEASE:run (default-cli) on project demo-jhipster: Unable to find a suitable main class, please add a 'mainClass' property -> [Help 1] org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.2.5.RELEASE:run (default-cli) on project demo-jhipster: Unable to find a suitable main class, please add a 'mainClass' property at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:216) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80) at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:355) at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155) at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584) at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:216) at org.apache.maven.cli.MavenCli.main(MavenCli.java:160) 

my parent pom and child pom

 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>spring-boot-starter-parent</artifactId> <groupId>org.springframework.boot</groupId> <version>1.2.5.RELEASE</version> <relativePath /> </parent> <groupId>com.aquevix.demo</groupId> <artifactId>demo-jhipster</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>demo_jhipster</name> <prerequisites> <maven>3.0.0</maven> </prerequisites> <modules> <module>JhipsterSubmodule</module> </modules> <properties> <assertj-core.version>3.1.0</assertj-core.version> <awaitility.version>1.4.0</awaitility.version> <commons-io.version>2.4</commons-io.version> <commons-lang.version>2.6</commons-lang.version> <gatling-maven-plugin.version>2.1.6</gatling-maven-plugin.version> <gatling.version>2.1.6</gatling.version> <hibernate.version>4.3.10.Final</hibernate.version> <hikaricp.version>2.4.0</hikaricp.version> <jacoco-maven-plugin.version>0.7.4.201502262128</jacoco-maven-plugin.version> <java.version>1.8</java.version> <javax.inject.version>1</javax.inject.version> <joda-time-hibernate.version>1.4</joda-time-hibernate.version> <liquibase-hibernate4.version>3.5</liquibase-hibernate4.version> <liquibase-slf4j.version>1.2.1</liquibase-slf4j.version> <liquibase.version>3.4.1</liquibase.version> <mapstruct.version>1.0.0.CR1</mapstruct.version> <maven-enforcer-plugin.version>1.3.1</maven-enforcer-plugin.version> <maven-sortpom-plugin.version>2.3.0</maven-sortpom-plugin.version> <maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format> <maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.target>${java.version}</maven.compiler.target> <metrics-spark-reporter.version>1.2</metrics-spark-reporter.version> <metrics-spring.version>3.1.2</metrics-spring.version> <!-- Sonar properties --> <project.testresult.directory>${project.build.directory}/test-results</project.testresult.directory> <run.addResources>false</run.addResources> <sonar-maven-plugin.version>2.6</sonar-maven-plugin.version> <sonar.exclusions>src/main/webapp/assets/**/*.*, src/main/webapp/bower_components/**/*.*, src/main/webapp/dist/**/*.*</sonar.exclusions> <sonar.jacoco.itReportPath>${project.testresult.directory}/coverage/jacoco/jacoco-it.exec</sonar.jacoco.itReportPath> <sonar.jacoco.reportPath>${project.testresult.directory}/coverage/jacoco/jacoco.exec</sonar.jacoco.reportPath> <sonar.java.codeCoveragePlugin>jacoco</sonar.java.codeCoveragePlugin> <sonar.javascript.jstestdriver.reportsPath>${project.testresult.directory}/karma</sonar.javascript.jstestdriver.reportsPath> <sonar.javascript.lcov.reportPath>${project.testresult.directory}/coverage/report-lcov/lcov.info</sonar.javascript.lcov.reportPath> <sonar.sources>${project.basedir}/src/main/</sonar.sources> <sonar.surefire.reportsPath>${project.testresult.directory}/surefire-reports</sonar.surefire.reportsPath> <sonar.tests>${project.basedir}/src/test/</sonar.tests> <spring-security.version>4.0.2.RELEASE</spring-security.version> <springfox.version>2.0.3</springfox.version> <usertype-core.version>4.0.0.GA</usertype-core.version> </properties> <dependencies> // dependencies.. </dependencies> <build> <resources> <resource> <filtering>true</filtering> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <filtering>false</filtering> <directory>src/main/resources</directory> <excludes> <exclude>**/*.xml</exclude> </excludes> </resource> </resources> <plugins> <plugin> <groupId>com.google.code.sortpom</groupId> <artifactId>maven-sortpom-plugin</artifactId> <version>${maven-sortpom-plugin.version}</version> <executions> <execution> <phase>verify</phase> <goals> <goal>sort</goal> </goals> </execution> </executions> <configuration> <sortProperties>true</sortProperties> <nrOfIndentSpace>4</nrOfIndentSpace> <sortDependencies>groupId,artifactId</sortDependencies> <sortPlugins>groupId,artifactId</sortPlugins> <keepBlankLines>true</keepBlankLines> <expandEmptyElements>false</expandEmptyElements> </configuration> </plugin> <plugin> <groupId>io.gatling</groupId> <artifactId>gatling-maven-plugin</artifactId> <version>${gatling-maven-plugin.version}</version> <configuration> <configFolder>src/test/gatling/conf</configFolder> <dataFolder>src/test/gatling/data</dataFolder> <resultsFolder>target/gatling/results</resultsFolder> <bodiesFolder>src/test/gatling/bodies</bodiesFolder> <simulationsFolder>src/test/gatling/simulations</simulationsFolder> <!-- This will force Gatling to ask which simulation to run This is useful when you have multiple simulations --> <simulationClass>*</simulationClass> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-eclipse-plugin</artifactId> <configuration> <downloadSources>true</downloadSources> <downloadJavadocs>true</downloadJavadocs> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-enforcer-plugin</artifactId> <version>${maven-enforcer-plugin.version}</version> <executions> <execution> <id>enforce-versions</id> <goals> <goal>enforce</goal> </goals> </execution> </executions> <configuration> <rules> <requireMavenVersion> <message>You are running an older version of Maven. JHipster requires at least Maven 3.0</message> <version>[3.0.0,)</version> </requireMavenVersion> <requireJavaVersion> <message>You are running an older version of Java. JHipster requires at least JDK ${java.version}</message> <version>[${java.version}.0,)</version> </requireJavaVersion> </rules> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <argLine>-Xmx256m ${surefireArgLine}</argLine> <!-- Force alphabetical order to have a reproducible build --> <runOrder>alphabetical</runOrder> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <packagingExcludes>WEB-INF/lib/tomcat-*.jar</packagingExcludes> </configuration> </plugin> <plugin> <groupId>org.bsc.maven</groupId> <artifactId>maven-processor-plugin</artifactId> <version>2.2.4</version> <executions> <execution> <id>process</id> <phase>generate-sources</phase> <goals> <goal>process</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>${mapstruct.version}</version> </dependency> </dependencies> <configuration> <defaultOutputDirectory>${project.build.directory}/generated-sources</defaultOutputDirectory> <processors> <processor>org.mapstruct.ap.MappingProcessor</processor> </processors> <options> <mapstruct.suppressGeneratorTimestamp>true</mapstruct.suppressGeneratorTimestamp> <mapstruct.defaultComponentModel>spring</mapstruct.defaultComponentModel> </options> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>sonar-maven-plugin</artifactId> <version>${sonar-maven-plugin.version}</version> </plugin> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>${jacoco-maven-plugin.version}</version> <executions> <execution> <id>pre-unit-tests</id> <goals> <goal>prepare-agent</goal> </goals> <configuration> <!-- Sets the path to the file which contains the execution data. --> <destFile>${project.testresult.directory}/coverage/jacoco/jacoco.exec</destFile> <!-- Sets the name of the property containing the settings for JaCoCo runtime agent. --> <propertyName>surefireArgLine</propertyName> </configuration> </execution> <!-- Ensures that the code coverage report for unit tests is created after unit tests have been run --> <execution> <id>post-unit-test</id> <phase>test</phase> <goals> <goal>report</goal> </goals> <configuration> <dataFile>${project.testresult.directory}/coverage/jacoco/jacoco.exec</dataFile> <outputDirectory>${project.testresult.directory}/coverage/jacoco</outputDirectory> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.liquibase</groupId> <artifactId>liquibase-maven-plugin</artifactId> <version>${liquibase.version}</version> <dependencies> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.18.2-GA</version> </dependency> <dependency> <groupId>org.liquibase.ext</groupId> <artifactId>liquibase-hibernate4</artifactId> <version>${liquibase-hibernate4.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>${project.parent.version}</version> </dependency> </dependencies> <configuration> <changeLogFile>src/main/resources/config/liquibase/master.xml</changeLogFile> <diffChangeLogFile>src/main/resources/config/liquibase/changelog/${maven.build.timestamp}_changelog.xml</diffChangeLogFile> <driver /> <url /> <defaultSchemaName /> <username /> <password /> <referenceUrl>hibernate:spring:com.aquevix.demo.domain?dialect=</referenceUrl> <verbose>true</verbose> <logging>debug</logging> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <jvmArguments>-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005</jvmArguments> <arguments> <argument>--spring.profiles.active=dev</argument> </arguments> </configuration> </plugin> </plugins> </build> <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <!-- log configuration --> <logback.loglevel>DEBUG</logback.loglevel> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> </dependencies> </profile> <profile> <id>fast</id> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <arguments> <argument>--spring.profiles.active=dev,fast</argument> </arguments> </configuration> </plugin> </plugins> </build> <properties> <!-- log configuration --> <logback.loglevel>DEBUG</logback.loglevel> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency> </dependencies> </profile> <profile> <id>prod</id> <build> <plugins> <plugin> <groupId>com.github.trecloux</groupId> <artifactId>yeoman-maven-plugin</artifactId> <version>0.4</version> <executions> <execution> <id>run-frontend-build</id> <phase>generate-resources</phase> <goals> <goal>build</goal> </goals> <configuration> <buildTool>grunt</buildTool> <buildArgs>build --force --no-color</buildArgs> </configuration> </execution> </executions> <configuration> <yeomanProjectDirectory>${project.basedir}</yeomanProjectDirectory> </configuration> </plugin> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>2.5</version> <configuration> <filesets> <fileset> <directory>src/main/webapp/dist</directory> </fileset> <fileset> <directory>.tmp</directory> </fileset> <fileset> <directory>node_modules</directory> </fileset> </filesets> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <arguments> <argument>--spring.profiles.active=prod</argument> </arguments> </configuration> </plugin> </plugins> </build> <properties> <!-- log configuration --> <logback.loglevel>INFO</logback.loglevel> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> </dependencies> </profile> </profiles> </project> 

and chile pom

 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>demo-jhipster</artifactId> <groupId>com.aquevix.demo</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>JhipsterSubmodule1</artifactId> </project> 

I have googled, but not a single solution was found, please help in this regard!

enter image description here

+7
java spring maven
source share
10 answers

I find that the problem I am facing is similar to yours. I have an application with several modules using Spring Boot and only one Application.java (with the main method).

I struggled with this, trying to run it using maven, and I got the same result as yours (could not find a suitable main class).

I spent several hours trying and changing things, and after adding the mainClass property to pom, he could not find it.

Then I decided to start the project not from the root project, but from the project where the Application class is located.

By doing this, I was able to start Spring boot, and everything works like a charm.

------- added on 2017/08/09

If you want to pack everything that needs to be done using java -jar file , you can run the command below from the root project:

 mvn clean package spring-boot:repackage 

Remember to add the spring-boot-maven plugin to your pom.

+14
source share

Extract the maven build plugin code from the parent pom, which will be similar to the one below, the snippet below should only be in the sub module, where you have the main class for the spring boot application:

  <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> 
+4
source share

You have a class in your application with the main method, for example:

 public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } 

Or do you even have several classes with basic methods, in which case Spring cannot determine which one to use?

+2
source share

You may have forgotten to specify the starting class.

In pom.xml:

 <properties> <start-class>com.ishanitech.controller.Application</start-class> </properties> 
+2
source share

You can fix the problem by specifying the location of the main device as follows:

  mvn spring-boot {com.ishanitech.controller.Application}:run 
0
source share
 mvn clean spring-boot:start 

... will give you the same error.

 mvn clean package spring-boot:start 

... is about to fix the problem by creating an artifact before running sprint-boot:start .

0
source share

If you are trying to run mvn spring-boot:run or just mvn (as you seem to be using JHipster, and that is how they bind it as the default target) in the root of your project, this is fine if I think it can only be used in a regular spring boot project.

It seems that your parent project has a number of Spring-Boot webapps, I believe that each of them has its own main () startup class, and that you just use the parent pom to factorize your pops and you can pack all your applications individually using one team.

So, in this case, just run mvn package , not a mvn and a mvn spring-boot:run .

0
source share

I also ran into the same problem, but adding the line below to my pom.xml, I solved the problem for my project:

 <properties> <start-class>com.hls.om.wp.AgencySearch</start-class> </properties> 

where the AgencySearch class has the main method for launching the spring boot application.

0
source share

If you are using IntellJ IDEA , perhaps you can do it like this:

 <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>add.your.main.class.application.reference.here</mainClass> </configuration> </plugin> 

This solved my mistake.

0
source share

The solution is to change the pom.xml file

from this

 <packaging>pom</packaging> 

to that

 <packaging>war</packaging> 

or another value that you need, depending on your deployment method.

-2
source share

All Articles