"Unable to find main class" with Maven on spring-boot project in Eclipse
Asked Answered
P

25

120

I am pretty desperate since I no longer can compile my Maven projects. I have one logical project split into 7 maven modules which so far compiled fine, but today I introduced a feature, whereby I accidentally generated a circular maven dependency. Therefore I refactored the code and eliminated the circular dependency. Ever since I updated the pom files to reflect the fix my project doesn't build anymore. When I run maven with "-X clean install" I get the following java.lang.IllegalStateException: Unable to find main class

[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] agiletunes-parent
[INFO] agiletunes-util
[INFO] agiletunes-security-common
[INFO] agiletunes-marketing-persistence
[INFO] agiletunes-common-ui
[INFO] agiletunes-security-client
[INFO] agiletunes-marketing-application
[INFO] agiletunes-security-server
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building agiletunes-parent 0.0.1
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- spring-boot-maven-plugin:1.5.2.RELEASE:repackage (default) @ agiletunes-parent ---
[INFO] 
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ agiletunes-parent ---
[INFO] Installing C:\Users\Alexander\Documents\Development\agileTunes\agiletunes-parent\pom.xml to C:\Users\Alexander\Documents\Development\mavenArtifacts\com\agiletunes\agiletunes-parent\0.0.1\agiletunes-parent-0.0.1.pom
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building agiletunes-util 0.0.1
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ agiletunes-util ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Alexander\Documents\Development\agileTunes\agiletunes-util\src\main\resources
[INFO] skip non existing resourceDirectory C:\Users\Alexander\Documents\Development\agileTunes\agiletunes-util\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ agiletunes-util ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ agiletunes-util ---
[INFO] Not copying test resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ agiletunes-util ---
[INFO] Not compiling test sources
[INFO] 
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ agiletunes-util ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ agiletunes-util ---
[INFO] 
[INFO] --- spring-boot-maven-plugin:1.5.2.RELEASE:repackage (default) @ agiletunes-util ---
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] agiletunes-parent .................................. SUCCESS [  1.492 s]
[INFO] agiletunes-util .................................... FAILURE [  1.095 s]
[INFO] agiletunes-security-common ......................... SKIPPED
[INFO] agiletunes-marketing-persistence ................... SKIPPED
[INFO] agiletunes-common-ui ............................... SKIPPED
[INFO] agiletunes-security-client ......................... SKIPPED
[INFO] agiletunes-marketing-application ................... SKIPPED
[INFO] agiletunes-security-server ......................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.674 s
[INFO] Finished at: 2017-03-21T21:53:04+01:00
[INFO] Final Memory: 31M/309M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:repackage (default) on project agiletunes-util: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:repackage failed: Unable to find main class -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:repackage (default) on project agiletunes-util: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:repackage failed: Unable to find main class
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
    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:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.PluginExecutionException: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:repackage failed: Unable to find main class
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:145)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
    ... 20 more
Caused by: java.lang.IllegalStateException: Unable to find main class
    at org.springframework.boot.loader.tools.Repackager.buildManifest(Repackager.java:354)
    at org.springframework.boot.loader.tools.Repackager.repackage(Repackager.java:273)
    at org.springframework.boot.loader.tools.Repackager.repackage(Repackager.java:258)
    at org.springframework.boot.loader.tools.Repackager.repackage(Repackager.java:190)
    at org.springframework.boot.maven.RepackageMojo.repackage(RepackageMojo.java:212)
    at org.springframework.boot.maven.RepackageMojo.execute(RepackageMojo.java:199)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    ... 21 more
[ERROR] 
...

This is the pom.xml of my maven parent project:

<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>

<groupId>com.agiletunes</groupId>
<artifactId>agiletunes-parent</artifactId>
<version>0.0.1</version>

<packaging>pom</packaging>
<name>agiletunes-parent</name>
<description>Maven parent project of agiletunes</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
    <relativePath></relativePath>
</parent>

<modules>
    <module>../agiletunes-common-ui</module>
    <module>../agiletunes-marketing-application</module>
    <module>../agiletunes-marketing-persistence</module>
    <module>../agiletunes-security-client</module>
    <module>../agiletunes-security-common</module>
    <module>../agiletunes-security-server</module>
    <module>../agiletunes-util</module>
</modules>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <!-- Java -->
    <java.version>1.8</java.version>

    <!-- Spring -->
    <spring.boot.version>1.5.2.RELEASE</spring.boot.version>

    <!-- persistence -->
    <mysql-connector-java.version>6.0.5</mysql-connector-java.version>

    <!-- util -->
    <guava.version>17.0</guava.version>

    <!-- testing -->
    <mockito.version>1.9.5</mockito.version>

</properties>

<dependencies>

    <!-- Spring -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql-connector-java.version}</version><!--$NO-MVN-MAN-VER$ -->
        <scope>runtime</scope>
    </dependency>

    <!-- utils -->
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>${guava.version}</version>
    </dependency>

    <!-- unit test framework -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>${mockito.version}</version>
        <scope>test</scope>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

What do I need to do, to fix this? Please let me know, should you need additional information. Thank you in advance.

Pifer answered 21/3, 2017 at 20:30 Comment(4)
Possible duplicate of How do I tell Spring Boot which main class to use for the executable jar?Reisfield
What lets you think that it is a problem of the branch? I could go back, since I have a git versioning. But I wonder if the eclipse version is the problem?Pifer
Dear asettouf, thank you for the feedback. From the maven parent perspective I have a number of main methods since multiple - but not all - of the modules have a main as they can be used within another maven projects or standalone. I thought that this was doable with Maven and so far that was never a problem. The util module, were the built stops, has really no main. So, I am afraid the other post you pointed out, is not solving the issue since to my understanding it requires only one main method in the entire project.Pifer
Dear Ortomala Lokni. I hesitated to reset the branch, but you were right. After the reset everything works fine again. Thank you so much!Pifer
C
89

Just for someone who faced with similar problem. I added spring-boot-maven-plugin in parent POM in section pluginManagement section like this:

 <build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

Then I added the plugin only in the modules using it (in plugin section).The project has been successfully built after that.

Crass answered 1/4, 2019 at 16:27 Comment(1)
i was adding plugins without <pluginManagement?Enrobe
T
144

You should have this

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

only in those modules that you want to run, but not in the parent pom.

Threephase answered 22/3, 2017 at 15:41 Comment(5)
Mind explaining why doesn't it get inherited from parent pom and have to be exclusively included in child pom ?Vernissage
Because in a multi-module project, not all sub-modules should be executable.Kath
Thanks man! So this plugin defines which module is executable, right?Cobra
I did this one, still having same issueWatershed
Works like a charm when you want to write a lib!Osborn
C
89

Just for someone who faced with similar problem. I added spring-boot-maven-plugin in parent POM in section pluginManagement section like this:

 <build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

Then I added the plugin only in the modules using it (in plugin section).The project has been successfully built after that.

Crass answered 1/4, 2019 at 16:27 Comment(1)
i was adding plugins without <pluginManagement?Enrobe
K
64

if you don't have a main class in springboot project, you can execution spring-boot-maven-plugin by follow

         <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                </plugin>
            </plugins>
        </build>
Kimmy answered 11/6, 2019 at 2:4 Comment(1)
This is great answer when you don't have a main class in a non executable spring-boot app.Thank you.Educator
R
54

You can try adding this in the properties section of your pom

<start-class>your.package.main</start-class>
Ramshackle answered 21/3, 2017 at 21:0 Comment(3)
Oh no, it helped me but after deploying it, app crashes because of " Exception in thread "main" java.lang.ClassNotFoundException: my.package.main":(Nicosia
How to tell if jar is not an executable , rather a simple library jar or dependency jar. I am getting main class not found error on 2.1.3.RELEASE but its not comming on 2.0.8Constantine
In previous version, start-class was not required :(Nils
D
33

I had the same problem. It ended being a spring boot mvn plugin declaration on a subproject that does not actually run (no main class). I just removed the spring boot mvn plugin from the submodule and everything ran fine

Dodge answered 24/7, 2017 at 15:57 Comment(2)
Simple and good answer. It is worked for me as for no run library project.Lusty
What he means is remove <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> from parent POM and add it to POM of the runnable modules. Thanks ManDemurrage
Z
27

In project agiletunes-util pom.xml add

<properties>
   <spring-boot.repackage.skip>true</spring-boot.repackage.skip>
</properties>
Zandrazandt answered 5/3, 2020 at 4:9 Comment(0)
A
22

add property to maven properties like this

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <start-class>org.roshan.Application</start-class>
</properties>
Anaanabaena answered 22/3, 2017 at 18:46 Comment(1)
What if I do not have main class. just a library jar.Constantine
S
12

I had a similar problem in the pom.xml created for separating my data model into a new module.

Reading this tread I discovered I had accidentally put the <configuration><skip>true</skip></configuration> inside the <executions> block. Below is my corrected pom.xml build section. FYI, there is no <packaging> entry in my pom.xml.

Using Ubuntu 20.04 LTS, Java 11.0.7, maven 3.6.3, Spring Starter 2.3.2.RELEASE, IntelliJ 2020.2

Working build configuration for pom.xml of my data model module

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
Sauger answered 4/8, 2020 at 21:27 Comment(1)
Thanks, worked for me. Had the same problem.Eb
M
6

I solved that,

Add the following code to pom file,

<profiles>
    <profile>
        <id>profile1</id>
        <properties>
            <spring.boot.mainclass>com.detaysoft.Application</spring.boot.mainclass>
        </properties>
    </profile>
</profiles>

Then build the build tag as follows,

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <mainClass>${spring.boot.mainclass}</mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Montelongo answered 17/9, 2017 at 10:54 Comment(1)
It only worked when the configuration tag was out side instead of under executions.Odontograph
C
4

Wouldn't you simply tell it by using the annotation @SpringBootApplication?

https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-using-springbootapplication-annotation.html

Many Spring Boot developers always have their main class annotated with @Configuration, @EnableAutoConfiguration and @ComponentScan. Since these annotations are so frequently used together (especially if you follow the best practices above), Spring Boot provides a convenient @SpringBootApplication alternative.

Cicelycicenia answered 13/7, 2017 at 15:30 Comment(0)
R
4

If you have multi modules maven project, as first workaround simply disable repackage to avoid this exception :

<properties>
    <spring-boot.repackage.skip>true</spring-boot.repackage.skip>
</properties>

Other workarouds :

<executions>
      <execution>
          <goals>
               <goal>repackage</goal>
           </goals>
      </execution>
</executions>
<configuration>
      <skip>true</skip>
</configuration>

As solution:

<properties>
 <spring.boot.mainclass>ir.shaparak.Application</spring.boot.mainclass>
</properties>

Another solution: In parent POM do as follows:

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

In sub modules(runable moudles) should be as follows

   <build>
          <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
           </plugins>
    </build>
Rachellrachelle answered 15/2, 2021 at 11:17 Comment(0)
P
3

I was also facing the same issue is resolved by adding <id>repackage</id>.

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <skip>true</skip>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
Pensioner answered 27/5, 2021 at 13:23 Comment(0)
A
3

I had the same issue for multi module project and I added <mainClass>package-name-in-which-main-method-resides</mainClass> and it worked.

<build>
<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
    <mainClass>package-name-where-@SpringBootApplication-main-method-is(com.agiletunes)</mainClass>
    </configuration>
    </plugin>
</plugins>
</build>
A answered 18/8, 2021 at 15:26 Comment(0)
B
2

I also stumbled across this issue, and it turned out that packaging type was wrong. When I changed packaging from pom to jar, everything started to work correctly.

Barometry answered 21/6, 2020 at 13:13 Comment(0)
K
1

In my case I had to add the "sourceDirectory" and my pom.xml changed from:

...

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

...

to

...

<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

...

Kesselring answered 7/2, 2019 at 17:56 Comment(0)
F
1

In my case, I had a code source file named java even though I was explicitly using kotlin:

<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
Firstrate answered 25/5, 2020 at 14:33 Comment(0)
C
1

In my case, I had missed bellow tag in progect pom:

<packaging>jar</packaging>

or

<packaging>war</packaging>

so, I added and it worked fine.

Corenecoreopsis answered 4/6, 2021 at 12:14 Comment(0)
I
1

In my case, I had a kotlin project but the src dir was named java instead of kotlin by the IDE. refactoring the name fixed the issue.

Imposition answered 5/9, 2021 at 7:51 Comment(0)
P
1

under

<artifactId></artifactId>

tag write this property

    <properties>
        <spring-boot.repackage.skip>true</spring-boot.repackage.skip>
    </properties>

it is helped me

Popular answered 21/5, 2022 at 14:9 Comment(0)
T
1
<properties>
  <spring-boot.repackage.skip>true</spring-boot.repackage.skip>
</properties>

This will work for your pom.xml as your program is trying to find a main class so that it can repackage all the dependencies into a single fat jar but if your module does not contain a main class then you need to specify it with the above code in your module's pom file.

Thermoelectrometer answered 30/10, 2022 at 20:13 Comment(0)
A
0

If you manually build your Sprint Boot project by using maven then make sure your project has a class like " class Application extends SpringBootServletInitializer " with main method in that.

Aceves answered 1/5, 2019 at 23:13 Comment(0)
S
0

With Maven you can tell which is the main class by:

<build>
  <plugins>
    <plugin>
      <configuration>
        <mainClass>com.xxx.yyy.mainApplication<mainClass>
      <configuration>
    <plugin>
  <plugins>
</build>
Senarmontite answered 20/3, 2021 at 8:36 Comment(0)
V
0

In my case error was hidden under mistake in folder structure. It should be

src/main/java

but in my case I've made a mistake during files copy and ended up with

main/java

So, do not hesitate to check manually what is inside .jar built by spring-boot-maven-plugin. In my case it contains only spring classes and none of mine classes.

Versieversification answered 21/2 at 14:18 Comment(0)
P
-1

I also had same issue, and solved by adding Application class as

@SpringBootApplication
public class Application {

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

i missed adding application start point during configuration of project.

Prickly answered 21/11, 2019 at 5:35 Comment(0)
D
-2

There are 2 ways to solve the issue:

  1. Create Main class:
public class Main {public static void main(String[] args){}}
  1. Remove plugin from pom.xml:
<plugin>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
Dodi answered 22/5, 2020 at 3:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.