Java Spring Boot 程序找不到主类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28451120/
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
Spring Boot Program cannot find main class
提问by Christopher Jobling
I have a program which runs as a Spring boot App in eclipse. The program was running fine. Then i did the following:
我有一个在 Eclipse 中作为 Spring Boot App 运行的程序。该程序运行良好。然后我做了以下事情:
Right click on project -> Run As -> Maven Test .
右键单击项目 -> 运行方式 -> Maven 测试。
This was accidental. When i then tried to run the program as a spring boot app again, it threw the following error below.
这是偶然的。当我再次尝试将该程序作为 Spring Boot 应用程序运行时,它在下面抛出了以下错误。
Error: Could not find or load main class com.bt.collab.alu.api.webapp.Application
Error: Could not find or load main class com.bt.collab.alu.api.webapp.Application
How do i point the application back to my main class? Thanks
我如何将应用程序指向我的主类?谢谢
采纳答案by MariuszS
Main class is configurable in pom.xml
主类可在 pom.xml 中配置
<properties>
<start-class>com.bt.collab.alu.api.webapp.Application</start-class>
</properties>
回答by thedoctor
Have a look under "Run -> Run Configurations..." in Eclipse. You should delete the new one which you created by mistake, you should still have the existing one.
查看 Eclipse 中的“运行 -> 运行配置...”。你应该删除你错误创建的新的,你应该仍然有现有的。
I suspect it has created a new run configuration for the "Run as Maven Test" and you are now always starting this one.
我怀疑它为“作为 Maven 测试运行”创建了一个新的运行配置,而您现在总是在启动这个配置。
回答by Tihamer
If you're using Spring Boot and the above suggestions don't work, you might want to look at the Eclipse Problems view (available at Window -> Show View -> Problems).
如果您使用的是 Spring Boot 并且上述建议不起作用,您可能需要查看 Eclipse Problems 视图(位于 Window -> Show View -> Problems)。
For example, you can get the same error (Error: Could not find or load main class groupId.Application) if one of your jar files is corrupted. Eclipse will complain that it can't find the Applications class, even though the bad jar is the root cause.
例如,如果您的 jar 文件之一已损坏,您可能会收到相同的错误(错误:无法找到或加载主类groupId.Application)。Eclipse 会抱怨它找不到 Applications 类,即使错误的 jar 是根本原因。
The Problems view, however, will identify the bad jar for you.
然而,问题视图将为您识别坏罐子。
At any rate, I had to manually go to my local mvn repo (in .m2) and manually delete the corrupted jar, and update it (right click on the project in the Package Explorer), Maven --> Update Project... -> OK (assuming that the correct project is check-marked).
无论如何,我必须手动转到本地 mvn 存储库(在 .m2 中)并手动删除损坏的 jar,然后更新它(右键单击包资源管理器中的项目),Maven --> 更新项目... -> OK(假设正确的项目被选中)。
回答by mantri
I had the same problem. Try this :
我有同样的问题。尝试这个 :
Right Click the project -> Maven -> Update Project
右键单击项目 -> Maven -> 更新项目
Then Re-run the project. Hope it work for you too.
然后重新运行项目。希望它也对你有用。
回答by Lakshmikanth c k
If you came across the
如果你遇到了
error:could not find or load main class in maven project
错误:无法在 Maven 项目中找到或加载主类
do this it worked for me:
Reason for the error: The reason for this error is the jar file or dependency to run a maven project is not installed correctly.
这样做对我
有用:错误原因:此错误的原因是运行 Maven 项目的 jar 文件或依赖项未正确安装。
Solution for the error: Go to the project folder (eg: D:\office\OIA-service-app
)
in the address bar of computer type command cmd
and press enter.
In the command prompt type command mvn clean install
make sure you have the internet connection.
解决方案错误:转到项目文件夹(如:D:\office\OIA-service-app
)
在计算机类型的命令的地址栏中cmd
,然后按Enter。
在命令提示符中键入命令mvn clean install
确保您有互联网连接。
回答by Techflash
Use spring-boot:runcommand to start spring boot application:
使用spring-boot:run命令启动 spring boot 应用程序:
Precondition:1. Add following property to pom.xml
前提条件:1. 在 pom.xml 中添加以下属性
<property>
<start-class>com.package.name.YourApplicationMainClass</start-class>
</property>
2. Build your project
2. 构建你的项目
Then configure maven command with spring-boot:run.
然后使用 spring-boot:run 配置 maven 命令。
Navigation:
导航:
Right Click Project | Run As | Run Configuration... | Add new Maven Configuration with command spring-boot:run
右键单击项目| 运行方式 | 运行配置... | 使用命令 spring-boot:run 添加新的 Maven 配置
回答by mertaksu
If your project packaging type war you could not start. I was using maven assembly plugin with this configuration;
如果您的项目打包类型战争,您将无法启动。我在这个配置中使用了 Maven 程序集插件;
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version><!--$NO-MVN-MAN-VER$ -->
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.ttech.VideoUploaderApplication</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
and my packaging tpe was war i got cannot find main class exception bu i changed packaging to jar it worked;
我的包装 tpe 是战争,我找不到主类异常,但我将包装更改为 jar 它可以工作;
<groupId>com.ttect</groupId>
<artifactId>VideoUploadListener</artifactId>
<version>1.0.0</version>
<packaging>**jar**</packaging>
回答by Kushwaha
I have used spring 1.5.3.RELEASE version and given pom dependencies like
我使用了 spring 1.5.3.RELEASE 版本并给出了 pom 依赖项,例如
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
And my main class was
我的主要课程是
@SpringBootApplication
public class SpringAppStarter {
public static void main(String[] args) {
SpringApplication.run(SpringAppStarter.class, args);
}
}
But still while execution of main class I was getting:
但是仍然在执行主类时我得到了:
Could not find or load main class ... with my class name.
无法使用我的类名找到或加载主类...。
I could see that class was available in the class path and I was using STS IDE.
我可以看到该类在类路径中可用,并且我使用的是 STS IDE。
I was not getting reason for the issue then I tried manual build through maven with
我没有得到这个问题的原因,然后我尝试通过 Maven 手动构建
mvn clean install
Still problem was there then i realize that my project has two libraries
仍然存在问题然后我意识到我的项目有两个库
- Reference Library
- Maven Library
- 参考图书馆
- Maven 库
What I did:
I have created another mavenRepo folder and build it again and refresh my project. After that it worked and I saw that Maven Library was deleted from project class path.
我做了什么:
我创建了另一个 mavenRepo 文件夹并再次构建它并刷新我的项目。之后它起作用了,我看到 Maven 库已从项目类路径中删除。
Issues was while execution it was getting multiple version so it was not able to find proper class to execute my application. Other Solution listed in this page also resolves the issue but if you are doing mistake in
问题是在执行时它获得了多个版本,因此无法找到合适的类来执行我的应用程序。此页面中列出的其他解决方案也可以解决该问题,但如果您在
- Annotating class
- pom entry
- jar version etc.
- 注释类
- pom 条目
- jar 版本等。
Hope this may resolve problem if somebody get this issue
如果有人遇到这个问题,希望这可以解决问题
回答by Sujeet
deleting old jars from .m2 and updating maven project should be primary step. In most of the cases it resolves the issue.The reason for this issue is mostly corrupted old jars
从 .m2 中删除旧 jar 并更新 maven 项目应该是主要步骤。在大多数情况下,它解决了问题。这个问题的原因主要是旧罐子损坏了
回答by amol
I am new in spring boot and using STS with maven build. I did all the things mentioned above but won't work for me. I noticed two problems in problem window of STS. There was a missing jar file on the path (C:\Users\UserName.m2\repository\com\jayway\jsonpath\json-path\2.2.0). I did following, the way is not strongly suggested but worked for me.
我是 Spring Boot 的新手,并在 maven 构建中使用 STS。我做了上面提到的所有事情,但对我不起作用。我注意到 STS 的问题窗口中有两个问题。路径上缺少 jar 文件 (C:\Users\UserName.m2\repository\com\jayway\jsonpath\json-path\2.2.0)。我做了以下,这种方式不是强烈建议,但对我有用。
1) I deleted .m2 folder from the path location
1)我从路径位置删除了 .m2 文件夹
2) Restarted STS
2) 重启 STS
3) Right Click on project folder -> maven -> update project -> checked "Force update of snapshots/releases" -> Click Ok.
3) 右键单击项目文件夹 -> maven -> 更新项目 -> 选中“强制更新快照/发布” -> 单击确定。
Maven updated its dependencies and the problem is solved.
Maven更新了依赖,问题解决了。