java Gradle构建rest spring应用程序找不到主类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28456023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:37:40  来源:igfitidea点击:

Gradle building rest spring app can't find main class

javaspringrestgradle

提问by Michal Taká?

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

我正在尝试做一个http://spring.io/guides/gs/rest-service/教程,我做了一切就像在教程中一样。

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

当我尝试使用教程中的 gradle.build 构建 gradle 时,由于丢失,gradle 构建失败

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

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?

我不知道该怎么办。有什么帮助吗?

回答by Opal

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

应该是hello.Applicationmain/java是包名/项目目录结构的一部分。

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

将以下代码添加到build.gradle 时

springBoot {
  mainClass = "hello.Application"
}

both ./gradlew clean bootRunand ./gradlew clean buildwith java -jar build/libs/gs-rest-service-0.1.0.jarwork well.

两者./gradlew clean bootRun./gradlew clean buildjava -jar build/libs/gs-rest-service-0.1.0.jar工作做好。

回答by Muhammad Asif

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.jarfile because of the src/main/java/hello, folder is not under the gradle build scope.

上述错误是由于gs-rest-service-0.1.0.jarsrc/main/java/hello, 文件夹不在 gradle 构建范围内,因此构建没有将我们的 Web RESTful Service 主应用程序类文件包含到文件中。

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

为了避免上述错误或https://spring.io/guides/gs/rest-service/教程中的任何其他错误

Please follow the steps below.

请按照以下步骤操作。

My Folder structure as follows.

我的文件夹结构如下。

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

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

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

将您的 build.gradle 文件放在您的主文件夹下,例如"MyWebService"不在您的 hello 或任何其他文件夹中,因此“gradle build”将成功。

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

使用 DOS cmd 导航到您的主文件夹e.g C:\MyWebService\,其中 src 应该是第一个子文件夹。

Run the gradle commands.

运行 gradle 命令。

gradle

gradle tasks

gradle wrapper

gradlew clean build -- final build

or gradlew clean bootRun -- run before build

等级

毕业任务

gradle 包装器

gradlew clean build -- 最终构建

或 gradlew clean bootRun -- 在构建之前运行

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

你会发现你gs-rest-service-0.1.0.jarC:\MyWebService\build\libs folder.

Finally invoke spring web service from main folder e.g C:\MyWebService\

最后从主文件夹调用 spring web 服务e.g C:\MyWebService\

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

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.

要通过在浏览器中点击以下 url 来检查 Spring RESTful Web Service,将返回 JSON 数据。

http://localhost:8080/greeting

http://localhost:8080/greeting

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

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

现在您应该成功完成本Spring RESTful Web Service教程。

N.B: Please do not modify your original build.gradlefile provided on the tutorial.

注意:请不要修改build.gradle教程中提供的原始文件。

回答by Mani

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

在 build.gradle 中添加上面突出显示的行