Java 无法找到主类:bootRepackage
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22528114/
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
Unable to find main class :bootRepackage
提问by mfrachet
I got a problem with my gradle build. I use the standard proposed by the Spring Website (https://spring.io/guides/gs/rest-service/), but when I try to use gradle build, I got this error :
我的 gradle 构建有问题。我使用 Spring 网站 ( https://spring.io/guides/gs/rest-service/)提出的标准,但是当我尝试使用 gradle build 时,出现此错误:
It doesnt work for this gradle, but when I use another one (that I took when I was at school) it work perfectly.
它不适用于这个 gradle,但是当我使用另一个(我在学校时拿的)时,它完美地工作。
回答by Dave Syer
There is no main method in your project (otherwise the plugin would find one). A main method has a very specific signature, so check that you have public static void main(String[] args)
.
您的项目中没有 main 方法(否则插件会找到一个)。main 方法具有非常具体的签名,因此请检查您是否拥有public static void main(String[] args)
.
回答by Pradeep
There are two possibilities
有两种可能
- Your source directory is not in the right location (use the sourceSets directive to fix this. your source directory should resemble something like
src/main/java/your/package
) Add this to indicate where your main class is
springBoot { mainClass = "hello.FileUploader" }
- 你的源目录不在正确的位置(使用 sourceSets 指令来解决这个问题。你的源目录应该类似于
src/main/java/your/package
) 添加此项以指示您的主类在哪里
springBoot { mainClass = "hello.FileUploader" }
I am pretty sure it is 1.
我很确定它是 1。
回答by Richard
If the main class is not defined in the current project which the build.gradle file belongs to, but you want to launch it for some purpose, like sprint integration test. Do it like this:
如果 build.gradle 文件所属的当前项目中未定义主类,但您想出于某种目的启动它,例如 sprint 集成测试。像这样做:
Adding
添加
bootRepackage {
mainClass = 'your.app.package.Application'
}
in build.gradle (after the line apply plugin: 'spring-boot', because the plugin needs to be loaded) fix the problem.
在 build.gradle 中(在 apply plugin: 'spring-boot' 之后,因为需要加载插件)修复问题。
回答by Pramit Sinha
I know this is a very old post. But I came across this issue while trying to build my first spring- boot application (https://spring.io/guides/gs/spring-boot/#scratch). So the location of the pom.xml, mentioned in the tutorial is incorrect. You need to place it outside your src folder. So here is the final directory structure - /workspace/src/main/java/hello/HelloController.java /workspace/src/main/java/hello/Application.java /workspace/pom.xml
我知道这是一个很老的帖子。但是我在尝试构建我的第一个 spring-boot 应用程序 ( https://spring.io/guides/gs/spring-boot/#scratch) 时遇到了这个问题。所以教程中提到的 pom.xml 的位置是不正确的。您需要将它放在 src 文件夹之外。所以这里是最终的目录结构 - /workspace/src/main/java/hello/HelloController.java /workspace/src/main/java/hello/Application.java /workspace/pom.xml
回答by surga
I solved it by specifying the encoding. Probably it's because I wrote the code in an IDE.
我通过指定编码解决了它。可能是因为我在 IDE 中编写了代码。
java -Dfile.encoding=UTF-8 -jar build <filename>.jar
回答by Bruce
I also have this problem, Here I solved the problem:
我也有这个问题,我在这里解决了这个问题:
use org.springframework.boot:spring-boot-starter
instead of org.springframework.boot:spring-boot-starter-web
, if your project is only an module that will be used in other project.
如果您的项目只是一个将在其他项目中使用的模块,请使用org.springframework.boot:spring-boot-starter
代替org.springframework.boot:spring-boot-starter-web
。
Or Set the main class in gradle:
或者在gradle中设置主类:
mainClassName = 'your.package.MainClass'
Or Just disable the bootRepackage
或者只是禁用 bootRepackage
bootRepackage {
enabled = false
}
回答by Gidi
This happened to me as well.
这也发生在我身上。
I was confused by the location of build.gradle
file: I thought it should be located in src/main/java/hello, because it is mentioned right after the instruction to create this sub-directory structure.
我对build.gradle
文件的位置感到困惑:我认为它应该位于src/main/java/hello,因为在创建此子目录结构的指令之后提到了它。
It should be placed in the root folder containing srcfolder. Once I did that and called "gradle build
" from the root folder and not "./gradlew build
" as the guide instructs, build was successful.
它应该放在包含src文件夹的根文件夹中。一旦我这样做并gradle build
从根文件夹中调用“ ”而不是./gradlew build
指南指示的“ ”,构建就成功了。
I did not perform the standard installation for gradle, just downloaded the binaries, maybe this is the reason that "./gradlew build
" failed for me.
我没有执行gradle的标准安装,只是下载了二进制文件,也许这就是“ ./gradlew build
”失败的原因。