Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle building rest spring app can't find main class

I'm trying to do a http://spring.io/guides/gs/rest-service/ tutorial, and I did everything like it is in tutorial.

When I was trying to build with gradle with the gradle.build from the tutorial gradle build failed because of missing

springBoot {
  mainClass = "main.java.hello.Application"
}

I did add it and now compilation start and finish correctly, but as soon as I'm trying to do

java -jar build/libs/gs-rest-service-0.1.0.jar

It throws an error enter image description here

I have no idea what to do with it. Any help?

like image 644
Michal Takáč Avatar asked Feb 11 '15 13:02

Michal Takáč


People also ask

Where is the main class in spring boot application?

A Spring Boot application's main class is a class that contains a public static void main() method that starts up the Spring ApplicationContext. By default, if the main class isn't explicitly specified, Spring will search for one in the classpath at compile time and fail to start if none or multiple of them are found.

Does spring boot need a main method?

Method main is an entry point for standalone applications, so if you want to use spring boot standalone application, usually (if not always) packaged into a JAR - then yes, you should use main method. If you work with Web Applications, however, they don't have main method.

Can we have two main class in spring boot?

Spring Boot allows us to define the Main class in the configuration when we have multiple main classes declared in the application. As we are using a MAVEN build, we have to configure the POM.


1 Answers

It should be hello.Application. main/java is a part of package name / project dir structure.

When added the following piece of code to build.gradle:

springBoot {
  mainClass = "hello.Application"
}

both ./gradlew clean bootRun and ./gradlew clean build with java -jar build/libs/gs-rest-service-0.1.0.jar work well.

like image 117
Opal Avatar answered Sep 18 '22 20:09

Opal