Issue
Following Spring-Boot tutorial, try to run with $ mvn spring-boot:run, gives me and error: "Unable to find a suitable main class..." How do I move past this?
(Working with Ubuntu)
I have followed this tutorial to try and work with Spring-Boot:
up until 11.4. When I enter
$ mvn spring-boot:run
I end up with this error:
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.3.RELEASE:run (default-cli) on project myproject: Unable to find a suitable main class, please add a 'mainClass' property
I thought I'd followed every direction to the T. The Example.java, which is supposed to be running at this step, is located here,
folder/src/main/java/Example.java
The one called "folder" has the pom file for maven, and everything else. Maven doesn't have any problem reading that pom file.
From the stack trace:
Caused by: org.apache.maven.plugin.MojoExecutionException: Unable to find a suitable main class, please add a 'mainClass' property
What do I do? All the other questions seem to have to do with the directory structure, or something more complicated. Where did I go wrong here?
Solution
I dont know what you did, it appears that you dont have a main function to start the application. I just made a project folder, then I created the pom.xml file with
<?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.1.3.RELEASE</version>
</parent>
<!-- Additional lines to be added here... -->
</project>
Then I modified my pom.xml to the content below
<?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.1.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- Additional lines to be added here... -->
</project>
And created my project/src/main/java/Example.java as follows (equals to the Exmaple.java from the reference)
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration
public class Example {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(Example.class, args);
}
}
then I went to the project root directory and typed
$mvn spring-boot:run
and it worked.
Answered By - thiagoyf