简体   繁体   中英

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 在此输入图像描述

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

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.

The above error is due to the build do not includes our Web RESTful Service main application class files into the gs-rest-service-0.1.0.jar file because of the src/main/java/hello, folder is not under the gradle build scope.

In order to avoid the above error or any other errors for https://spring.io/guides/gs/rest-service/ tutorial

Please follow the steps below.

My Folder structure as follows.

C:\\MyWebService\\src\\main\\java\\hello

Put your build.gradle file under your main folder eg "MyWebService" not in your hello or any other folder hence "gradle build' will be successful.

Using DOS cmd navigate to your main folder eg C:\\MyWebService\\ where src should be the first sub folder.

Run the gradle commands.

gradle

gradle tasks

gradle wrapper

gradlew clean build -- final build

or gradlew clean bootRun -- run before build

You will find your gs-rest-service-0.1.0.jar under your C:\\MyWebService\\build\\libs folder.

Finally invoke spring web service from main folder eg C:\\MyWebService\\

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

To check Spring RESTful Web Service by hitting below url in the browser, JSON data will be returned.

http://localhost:8080/greeting

{"id":1,"content":"Hello, World!"}

Now you should be successful with completing the Spring RESTful Web Service tutorial.

NB: Please do not modify your original build.gradle file provided on the tutorial.

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'application'

Add the above highlighted line in the build.gradle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM