Java Eclipse 错误:找不到或无法加载主类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16334625/
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-08-16 06:22:06  来源:igfitidea点击:

Eclipse Error: Could not find or load main class

javaeclipse

提问by Fezzo

Have Googled extensively on this error, but I can't seem to fix the problem. I've written a basic java program in Eclipse Juno, as follows:

在谷歌上广泛搜索了这个错误,但我似乎无法解决这个问题。我已经在Eclipse Juno中编写了一个基本的java程序,如下:

public class HelloWorld {

    /**
     * @param args
     */

    public static void main(String[] args) {
        System.out.println("Hello Eclipse!");
    }

}

After clicking Run, I get: "Error: Could not find or load main class HelloWorld". This error message applies to all my other projects in the same workspace. I've tried switching workspaces but the error still appears.

单击运行后,我收到:“错误:无法找到或加载主类 HelloWorld”。此错误消息适用于我在同一工作区中的所有其他项目。我试过切换工作区,但错误仍然出现。

I'm on Windows 7 64-bit. Any help would be appreciated!

我在 Windows 7 64 位上。任何帮助,将不胜感激!

回答by JRR

It seems that the class is not compiled by Eclipse.

似乎该类不是由 Eclipse 编译的。

Few pointers could be-

很少有指针可能是-

  1. Check if the .class file exists in your output folder.To know your output folder Right Click on Project->Properties->Java Build Path(Check at bottom).
  2. Check if Project->build Automaticallyis checked in the menu.
  3. Check if the HelloWorld class is in src folder or not.Right Click on Project->Properties->Java Build Path(Check source tab).
  1. 检查 .class 文件是否存在于您的输出文件夹中。要知道您的输出文件夹,请右键单击 Project->Properties->Java Build Path(检查底部)。
  2. 检查菜单中是否选中了Project->build Automatically
  3. 检查 HelloWorld 类是否在 src 文件夹中。右键单击Project->Properties->Java Build Path(检查源选项卡)。

回答by Akeme Ubom

This happened to me. I noticed that someone said I have to create an entire new WORKSHOP! Why? Because I installed a newer JRE version and that won't allow other previous versions to run on it. So all those old files I have become useless in a way. Not really, Just copy and paste it to new class and change it to an unused class name.

这发生在我身上。我注意到有人说我必须创建一个全新的WORKSHOP!为什么?因为我安装了较新的 JRE 版本,并且不允许其他以前的版本在其上运行。因此,所有这些旧文件在某种程度上都变得毫无用处。不是真的,只需将其复制并粘贴到新类并将其更改为未使用的类名。

回答by myluco

I just ran into that problem. The cause... not sure. However, only happened after I added a new jvm. My solution:

我刚刚遇到了这个问题。原因……不确定。然而,只有在我添加了一个新的 jvm 之后才发生。我的解决方案:

  • went to run configurations: - run->run configurations
  • 去运行配置:-运行- >运行配置

In the Classpath tab:

在“类路径”选项卡中:

  • Select Advanced
  • Add where Eclipse usually put the *.class for the projects, which is in bin. So I added the bin directory for the project.
  • 选择高级
  • 添加Eclipse 通常放置项目的*.class 的位置,即bin 中。所以我为项目添加了bin目录。

I hope it helps someone out there. It took me time to figure this out.

我希望它可以帮助那里的人。我花了一些时间来弄清楚这一点。

回答by Ahuramazda

that's because you guys created the class one time with the main method & after that may be you have been deleted that form or workplace & still some of the files exist ,i will suggest you to create that form or workspace again & then delete it by clicking on it completely,then after that if you created the some class like Runner class try to run it again.

那是因为你们用 main 方法创建了一次类,​​之后可能你已经删除了那个表单或工作区,仍然有一些文件存在,我建议你再次创建那个表单或工作区,然后删除它完全单击它,然后如果您创建了像 Runner 类这样的类,请尝试再次运行它。

回答by user2756160

if your package name is same with your class name this problem will occur.

如果您的包名与您的类名相同,则会出现此问题。

回答by lake

I deleted a jar file from the bin directory. Right click on your project - Properties then Libraries tab. There was a red flag in there. I removed the jar file from the Libraries and it worked.

我从 bin 目录中删除了一个 jar 文件。右键单击您的项目 - 属性,然后是库选项卡。那里有一面红旗。我从库中删除了 jar 文件并且它起作用了。

回答by Shashank Awasthi

Same Problem occur with me.I went to Project > Properties > Java BuildPath.

同样的问题发生在我身上。我去了项目 > 属性 > Java BuildPath。

There In order of export , I moved up my java/main to the top priority.

在那里为了 export ,我将我的 java/main 移到了最优先的位置。

回答by Nathan

This just happened to me today after updating my JRE. I cleaned the project and it started working again.

在更新我的 JRE 后,这只是今天发生在我身上。我清理了这个项目,它又开始工作了。

Project -> Cleanwill remove any existing class files and completely rebuild the project. There's more information on Eclipse's clean function here.

Project -> Clean将删除任何现有的类文件并完全重建项目。有一个关于Eclipse的清洁功能的详细信息在这里