Java Eclipse -> 错误:无法找到或加载主类 hello

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

Eclipse -> Error: Could not find or load main class hello

javaeclipseclasssdkcompilation

提问by mehulmpt

My code is easily compiling over CMD (I'm using Windows) but doesn't compile in eclipse.

我的代码很容易通过 CMD 编译(我使用的是 Windows),但不能在 Eclipse 中编译。

Please check the screenshot attached.

请检查所附的屏幕截图。

enter image description here

在此处输入图片说明

I read somewhere that I need to do some run configurations or add some JARS or external JARS but that all went over my head.

我在某处读到我需要做一些运行配置或添加一些 JARS 或外部 JARS,但这一切都超出了我的脑海。

Note: I'm new to java so please be a bit elaborated :) THANK YOU! :D

注意:我是 Java 新手,所以请详细说明一下 :) 谢谢!:D

Here is a screenshot of Problem tab in Eclipse: enter image description here

这是 Eclipse 中问题选项卡的屏幕截图: 在此处输入图片说明

采纳答案by Juned Ahsan

You need to mark your class as publicso that runtime is able to call & execute it.

您需要将您的类标记为,public以便运行时能够调用和执行它。

EDIT: So with the new image in your question, i can see that your JRE mapping is not correct under your project settings. Here is how you can update that:

编辑:因此,使用您问题中的新图像,我可以看到您的 JRE 映射在您的项目设置下不正确。以下是您可以更新的方法:

Right click on project -> Java Build Path -> Libraries tab -> double click listed JRE library -> select the appropriate JRE

右键单击项目 -> Java Build Path -> Libraries 选项卡 -> 双击列出的 JRE 库 -> 选择合适的 JRE

回答by Shriram

Where is your JRE? Add JRE by locating in windows. Click window preference and select installed JRE from java and choose jre environment.

你的 JRE 在哪里?通过在 windows 中定位来添加 JRE。单击窗口首选项并从 java 中选择已安装的 JRE 并选择 jre 环境。

回答by ARUN

Your Class name in Small Letters .Change the Name 'hello' in to Hello .

小写的班级名称。将名称 'hello' 更改为 Hello 。

You need to change your file name and class as "Hello"

您需要将文件名和类更改为“Hello”

回答by Nav

In my case, it was because there was a bug in Apache TomEE server. Going to Eclipse preferences and removing the TomEE server and replacing it with Tomcat 7 server and in the same dialog box, changing the JRE version to the jre7 which I installed (instead of choosing the workbench's JRE) helped solve the problem.

就我而言,这是因为 Apache TomEE 服务器中存在错误。转到 Eclipse 首选项并删除 TomEE 服务器并用 Tomcat 7 服务器替换它,在同一个对话框中,将 JRE 版本更改为我安装的 jre7(而不是选择工作台的 JRE)有助于解决问题。

回答by user1580096

I also ran into a similar problem on my Ubuntu 14.04 after updating my system. So what I did was simply created a new workspace and imported my projects in it. Then it started working again.

更新系统后,我在 Ubuntu 14.04 上也遇到了类似的问题。所以我所做的只是创建了一个新的工作区并将我的项目导入其中。然后它又开始工作了。

回答by Altaf Hussain Wani

Right click on project, go to properties/java compiler/enable project specific settings/select the jdk with which you want to compile, then check use default compilance setting. Now your program will compile without the error.

右键单击项目,转到属性/java编译器/启用项目特定设置/选择要编译的jdk,然后检查使用默认编译设置。现在您的程序将编译而不会出错。

回答by Bimales Mandal

Reason:

原因:

This error occurs when JVM unable to load class at runtime. Generally because in eclipse JDK compiler set to some older JDK version and eclipse is trying to run in latest JRE version.

当 JVM 无法在运行时加载类时会发生此错误。通常是因为在 eclipse 中 JDK 编译器设置为一些较旧的 JDK 版本,而 eclipse 试图在最新的 JRE 版本中运行。

Solution:

解决方案:

  • In eclipse preference (Java -> Compiler) please set appropriate JDK version.
  • In eclipse preference (Java -> Installed JREs) Please set same JRE.
  • 在 eclipse 首选项(Java -> Compiler)中,请设置适当的 JDK 版本。
  • 在 Eclipse 首选项(Java -> 已安装的 JRE)中,请设置相同的 JRE。

回答by Om Sao

enter image description hereRed exclaimation mark in your Package Explorer==> Project name shows that your whole project build has issue. It occurs when your eclipse/compiler is either pointing to wrong JRE/JDK path:

在此处输入图片说明Package Explorer ==> Project name 中的红色感叹号表明您的整个项目构建有问题。当您的 eclipse/编译器指向错误的 JRE/JDK 路径时,就会发生这种情况:

Here is the solution that worked form:

这是有效的解决方案:

  1. Recently you might have updated your system's JDK but your Eclipse project is still pointing to older version. Or you misplaced some of the .jar which is used by your project.
  1. 最近您可能已经更新了系统的 JDK,但您的 Eclipse 项目仍然指向旧版本。或者您放错了项目使用的一些 .jar 文件。

Right click on your project>>properties>>JavaBuildPath>>Libraries.

右键单击您的项目>>属性>>JavaBuildPath>>库。

Remove the jar which is having a red flag on it. If you still have the problem try below solution, this worked for me when i faced this problem second time:

取出上面有红旗的罐子。如果您仍然遇到问题,请尝试以下解决方案,当我第二次遇到此问题时,这对我有用:

right-click project>>Properties>>Java Build Path>>Libraries, Remove and Add Library. (Choose the JRE System Library )

右键单击项目>>属性>>Java构建路径>>库,删除和添加库。(选择 JRE 系统库)

回答by Sina Cengiz

I used to get this error. Basically there was a red cross sign on my Project name while I was getting this error. I deleted the project and defined a new project and the problem was solved just like that.

我曾经得到这个错误。基本上,当我收到此错误时,我的项目名称上有一个红十字标志。我删除了项目并定义了一个新项目,问题就这样解决了。

回答by reza

When you type the project name, select Use default JRE.

键入项目名称时,选择使用默认 JRE。