Java 无法在 STS/Eclipse 中找到或加载主类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37447721/
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
Could not find or load main class in STS/Eclipse
提问by aCarella
I have a main method in a package in one of my projects. Say, the package is com.ant.car
. I am trying to run and/or debug this main method, and I keep getting the error Could not find or load main class com.ant.car
.
我的一个项目的包中有一个主要方法。说,包裹是com.ant.car
。我正在尝试运行和/或调试此主要方法,但我不断收到错误消息Could not find or load main class com.ant.car
。
I've searched this problem, and it seems like I can't figure out what is wrong.
我已经搜索了这个问题,似乎我无法弄清楚出了什么问题。
1) I've checked run configurations. In Run->Run Configurations, I've checked that the Main class is com.ant.car.
1)我检查了运行配置。在 Run->Run Configurations 中,我检查了 Main 类是否为 com.ant.car。
2) I've checked build path. If I right click on the project, I select Build->Build Path, and under the Libraries tab I make sure there are no missing folders with red Xs next to them.
2)我检查了构建路径。如果我右键单击该项目,我会选择 Build->Build Path,然后在 Libraries 选项卡下确保没有丢失旁边带有红色 X 的文件夹。
Not really sure what else to do. Any suggestions?
不太确定还能做什么。有什么建议?
采纳答案by user2241925
Project -> Clean
项目 -> 清洁
this is working
这是工作
回答by user3624540
The Error "Could not find or load main class com.ant.car" occur when your class is not in build path. please make sure that .class file is generated and it's in build path ..
当您的类不在构建路径中时,会发生错误“无法找到或加载主类 com.ant.car”。请确保生成了 .class 文件并且它在构建路径中..
回答by aCarella
The solution to this was the following:
解决方案如下:
- Close Eclipse/STS
- Use a file explorer on your operating system to navigate to your workspace (In my case, I'm on Windows so I used Windows Explorer)
- Delete the
.metadata
directory (or to be safe, copy the directory somewhere else to be safe, then delete it) - Restart Eclipse/STS
- 关闭 Eclipse/STS
- 使用操作系统上的文件资源管理器导航到您的工作区(就我而言,我使用的是 Windows,所以我使用了 Windows 资源管理器)
- 删除
.metadata
目录(或为了安全起见,将目录复制到其他地方以确保安全,然后将其删除) - 重启 Eclipse/STS
Is there a more improved answer than this? I don't want to look like I'm trying to boost my own reputation points, so if someone can provide a better answer then please do so.
还有比这更好的答案吗?我不想看起来像我在努力提高自己的声誉点,所以如果有人可以提供更好的答案,那么请这样做。
回答by Arul Rozario
Sometimes the problem can be caused by some newly added dependencies. Try removing the recently added jars. That's how I fixed my problem.
有时问题可能是由一些新添加的依赖项引起的。尝试删除最近添加的罐子。这就是我解决我的问题的方法。
回答by Dhanu N Joy
I faced the same issue..
我遇到了同样的问题..
just do follow these steps:
只需按照以下步骤操作:
STS/Eclipse --> Project --> Enable "Build Automatically"
STS/Eclipse --> 项目 --> 启用“自动构建”
then refresh your project, it will resolve your issue.
然后刷新您的项目,它将解决您的问题。
Still not refreshed your projects automatically, just restart your STS and check.
仍然没有自动刷新您的项目,只需重新启动您的 STS 并检查。
Hope it will help you.
希望它会帮助你。
回答by Kiran Kumar D M
I think .class files are deleted/missing from JavaProject/bin folder.
我认为 .class 文件已从 JavaProject/bin 文件夹中删除/丢失。
To resolve this issue ->
要解决此问题->
1) Just cut paste and save the code contents of all the files that you are using then .class files will be regenerated.
1) 只需剪切粘贴并保存您正在使用的所有文件的代码内容,即可重新生成 .class 文件。
2) Then run the code and you can see it works fine if there is no syntactical errors.
2)然后运行代码,如果没有语法错误,您可以看到它工作正常。
回答by Matt Buckborough
Project -> Clean... -> check project not working -> Clean
项目 -> 清理... -> 检查项目不工作 -> 清理
I already had build automatically set, but forcing STS to rebuild it fixed it.
我已经自动设置了构建,但是强制 STS 重建它修复了它。
回答by Mariusz
Today I ran into the same problem and I tried a lot of answers. Nothing helped. Cleaning the project, build automatically is already checked, deleting .metadata
, etc.
今天我遇到了同样的问题,我尝试了很多答案。没有任何帮助。清理项目,自动构建已经被选中,删除.metadata
等。
Eventually I tried this and it worked perfectly:
最终我尝试了这个,它工作得很好:
menu Project -> Properties
Java Build Path, tab Libraries
菜单项目 -> 属性
Java 构建路径,选项卡库
Remove the JRE System Library from there, press Add Library
, take the JRE System Library and press Next
. Workspace default JRE
and press Finish
.
从那里删除 JRE 系统库,按Add Library
,取出 JRE 系统库并按Next
。工作区默认JRE
,然后按Finish
。
回答by user2615206
What worked for me: Menu Project -> PropertiesIn Java Build Path, tab LibrariesDelete all libraries with a red [x] next to them.
对我有用的是: Menu Project -> PropertiesIn Java Build Path,选项卡Libraries删除旁边带有红色 [x] 的所有库。
In my case, problem happened when I switched from Kepler to STS IDE.
就我而言,当我从 Kepler 切换到 STS IDE 时发生了问题。
回答by SelvaKumar T
Try updating project by right clicking on it. Maven -> Update Project
尝试通过右键单击来更新项目。Maven -> 更新项目