Java Netbeans - 错误:无法找到或加载主类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20034377/
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
Netbeans - Error: Could not find or load main class
提问by Matis Lepik
So I'm making a java application in Neatbeans 7.4, been working at it for a while, everything was fine, running the project worked fine, but now when I hit run project, I get the error
所以我在 Neatbeans 7.4 中制作了一个 Java 应用程序,已经工作了一段时间,一切都很好,运行项目工作正常,但是现在当我点击运行项目时,我收到错误
Error: Could not find or load main class phleveledit.MainWindow
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
I can't think of what I did right before this started happening, so I don't know what I should change.. The code doesn't appear to have any errors. Here's a screenshot of the IDE+project folder
我想不出在这开始发生之前我做了什么,所以我不知道我应该改变什么......代码似乎没有任何错误。这是IDE+项目文件夹的截图
Image (http://puu.sh/5ldYB) :
图片(http://puu.sh/5ldYB):
Any ideas?
有任何想法吗?
Edit: Unfortunately I happened to fix this problem by removing some code, which probably had some errors Netbeans couldn't detect, but I don't know what was exactly wrong so can't vote on a correct answer.
编辑:不幸的是,我碰巧通过删除一些代码来解决这个问题,这些代码可能有一些 Netbeans 无法检测到的错误,但我不知道到底出了什么问题,所以无法对正确答案进行投票。
回答by grepit
You can :
你可以 :
- RightClick on project node and go to Set configuration
- Select the main class for your application.
- Then clean and build.
- 右键单击项目节点并转到设置配置
- 为您的应用程序选择主类。
- 然后清理并构建。
Even if the above steps don't work for you then then delete the Netbeans cache by deleting the (index) folder
即使上述步骤对您不起作用,也可以通过删除(索引)文件夹来删除 Netbeans 缓存
User\.netbeans\SOME_NUMBER_reflecting_your_version\var\cache\index\
回答by Paul Samsotha
- Right click on your Project in the project explorer
- Click on properties
- Click on Run
- Make sure your Main Class is the one you want to be the entry point. (Make sure to use the fully qualified name i.e. mypackage.MyClass)
- Click OK.
- Clean an build your project
- Run Project :)
- 在项目资源管理器中右键单击您的项目
- 点击属性
- 点击运行
- 确保您的主类是您想要作为入口点的类。(确保使用完全限定名称,即 mypackage.MyClass)
- 单击确定。
- 清理构建您的项目
- 运行项目:)
If you just want to run the file, right click on the class from the package explorer, and click Run File, or (Alt+ R, F), or (Shift+ F6)
如果您只想运行该文件,请在包资源管理器中右键单击该类,然后单击“运行文件”,或 ( Alt+ R, F) 或 ( Shift+ F6)
回答by InCh
Sometimes due to out of memory space error, NetBeans does not load or find main class.
有时由于内存空间不足错误,NetBeans 不加载或找不到主类。
If you have tried setting the properties and still it is not working then try
如果您已尝试设置属性但仍然无法正常工作,请尝试
- Select the project from the project explorer
- Click on Run in the Menu Bar
- Click on Compile
- 从项目资源管理器中选择项目
- 单击菜单栏中的运行
- 点击编译
It worked for me.
它对我有用。
回答by Rohit Dodle
I had the same issue once. The problem was not in the code. The cause was... renaming the project folder to some other non supporting name. My project name was "MobStick" and I renamed it to "MobStick - May 26, 2014 04:00PM". Renaming it back to normal solved my problem.
我曾经遇到过同样的问题。问题不在代码中。原因是...将项目文件夹重命名为其他一些不支持的名称。我的项目名称是“MobStick”,我将其重命名为“MobStick - May 26, 2014 04:00PM”。将其重命名为正常解决了我的问题。
回答by RF Moore
This condition happens to me every 6-months or so. I think it happens when closing NetBeans under very low memory conditions. I discovered that it could be easily corrected by (1) Rename your project, including its folder name using right-click on project explorer's project name---I put a simple suffix on the original name ("_damaged"). (2) Try BUILD. If that is successful, which it is for me, give three cheers. (3) Repeat step (1) to restore the original project name. BUILD and RUN should start without trouble. I guess that the 'rename the project and folder' process causes a special rediscovery of the applications main location.
这种情况每 6 个月左右就会发生一次。我认为在非常低的内存条件下关闭 NetBeans 时会发生这种情况。我发现它可以很容易地通过(1)重命名您的项目,包括使用右键单击项目资源管理器的项目名称的文件夹名称---我在原始名称上添加一个简单的后缀(“_damaged”)。(2) 尝试构建。如果那是成功的,这对我来说是成功的,请三声欢呼。(3) 重复步骤(1),恢复原来的工程名称。BUILD 和 RUN 应该可以顺利启动。我猜想“重命名项目和文件夹”过程会导致对应用程序主位置的特殊重新发现。
回答by Evans Chikasa
Try to rename the package name and the class/jframe names... The clean and build the application.
尝试重命名包名称和类/jframe 名称...清理并构建应用程序。
- Right Click on the package name
- Go to Refactor
- Select Rename
- Give it a meaningful name, preferably all in small letters
Click on Refactor
Do the same for the class/jframe names.
- Last Select Run from Menu 7.Select Clean and build main project
- 右键单击包名称
- 去重构
- 选择重命名
- 给它一个有意义的名字,最好全部用小写字母
点击重构
对类/jframe 名称执行相同操作。
- 最后选择 Run from Menu 7.Select Clean and build main project
That should do it!!! All best
应该这样做!!!一切顺利
回答by Mary Martinez
I just ran into this problem. I was running my source from the command line and kept getting the same error. It turns out that I needed to remove the package name from my source code and then the command line compiler was happy.
我刚刚遇到了这个问题。我从命令行运行我的源代码并不断收到相同的错误。事实证明,我需要从我的源代码中删除包名,然后命令行编译器很高兴。
The solutions above didn't work for me so maybe this will work for someone else with a similar problem.
上面的解决方案对我不起作用,所以也许这对有类似问题的其他人有用。
回答by Everlasting
I have run into this error a couple of times as well and for me the above solutions did not work. What does seem to work is going to the Project Properties, and under Compiling toggling Compile on Save.
我也遇到过几次这个错误,对我来说上述解决方案不起作用。似乎可行的是转到“项目属性”,并在“编译”下切换“保存时编译”。
回答by bob
I had the same problem, I had the package and class named the same. I renamed the class, then clean and build. Then I set the main class in the "run" under the properties of the project. I works now.
我有同样的问题,我的包和类命名相同。我重命名了这个类,然后清理并构建。然后我在项目属性下的“运行”中设置主类。我现在工作。
回答by Eric Reed
I found the following steps useful:
我发现以下步骤很有用:
- Right-click on the project in the left toolbar.
- Hover over the 'Set Configuration' item.
- Click on 'Customize...'
- Click on 'Browse...' by the 'Main Class:' item.
- Select the correct class.
- Click 'Select Main Class'.
- Click 'OK'.
- 右键单击左侧工具栏中的项目。
- 将鼠标悬停在“设置配置”项目上。
- 单击“自定义...”
- 单击“主类:”项旁边的“浏览...”。
- 选择正确的班级。
- 单击“选择主类”。
- 单击“确定”。
My problem was that, apparently, my package name was being listed twice. Selecting the class using the dialog changed 'aclass.MainClass' to just 'MainClass'.
我的问题是,显然,我的包名被列出了两次。使用对话框选择类将“aclass.MainClass”更改为“MainClass”。
Hope this helps,
希望这可以帮助,
-HewwoCraziness
-HewwoCraziness
Edit: This is expanding on Mary Martinez's answer.
编辑:这是对玛丽马丁内斯的回答的扩展。