Daniel
Daniel

Reputation: 137

Maven Build Failure - Missing Main Class

I'm trying to get started with a Spring Boot Application with Maven. I've done the tutorial from: https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#getting-started

My pom.xml looks like this:

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

<groupId>com.example</groupId>
<artifactId>myproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
<!-- Additional lines to be added here... -->

<!-- (you don't need this if you are using a .RELEASE version) -->
<repositories>
    <repository>
        <id>spring-snapshots</id>
        <url>http://repo.spring.io/snapshot</url>
        <snapshots><enabled>true</enabled></snapshots>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <url>http://repo.spring.io/milestone</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-snapshots</id>
        <url>http://repo.spring.io/snapshot</url>
    </pluginRepository>
    <pluginRepository>
        <id>spring-milestones</id>
        <url>http://repo.spring.io/milestone</url>
    </pluginRepository>
</pluginRepositories>

My Project Folder looks like this: Test\src\main\java

Pom.xml is in Test\bin

There's only one Java file:

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
public class Example {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

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

}

If I run it with mvn spring-boot:run in cmd I'll get a build failure noting that it is unable to find a suitable main Class

Stacktrace:

[INFO] BUILD FAILURE
[INFO]      ------------------------------------------------------------------------
[INFO] Total time: 2.206 s
[INFO] Finished at: 2017-07-21T12:33:18+02:00
[INFO] Final Memory: 21M/227M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-     maven-plugin:2.0.0.BUILD-SNAPSHOT:run (default-cli) on project myproject:   Unable to find a suitable main class, please add a 'mainClass' property ->   [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions,   please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Upvotes: 1

Views: 2872

Answers (2)

ronhash
ronhash

Reputation: 874

Maven expected files to be in specific places: If your java file is in Test\src\main\java, then your pom file should be in Test, not in Test\bin

Just ran it with the same code and it builds fine.

You can read more about file location in maven here: https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

Upvotes: 1

davidxxx
davidxxx

Reputation: 131376

Your Example class is a valid Spring Boot Main class.

The problems is here :

Pom.xml is in Test\bin

Your pom.xml is not declared at the right place.

By default Maven expects to have the src\main\java folder at the same level where the pom.xml is defined.

So, with this layout, Maven tries to find your classes in

Test\bin\src\main\java

But your classes are here :

My Project Folder looks like this: Test\src\main\java

Just move the pom.xml at the root of the Test folder.

Upvotes: 2

Related Questions