Java错误:无法找到或加载主类 – Netbeans IDE

huangapple go评论88阅读模式
英文:

Java error: Could not find or load main class - Netbeans IDE

问题

在Netbeans IDE 8.2中制作Java程序时,当我尝试运行它时,出现了以下错误:

  1. Error: Could not find or load main class helloworld.HelloWorld
  2. C:\Users\name\AppData\Local\NetBeans\Cache.2\executor-snippets\run.xml:53: Java returned: 1
  3. BUILD FAILED (total time: 1 second)

(假设helloworld是我的项目名称,HelloWorld是我的类名)

在这之前一切都很正常,我不知道我做了什么会导致这个错误发生。这是我第一次遇到这个错误,所以我不知道该怎么办。代码似乎没有任何错误。

即使我直接在主类class6homework.java中键入代码,错误仍然会发生,但错误消息会说class6homework.class6homework,而不是屏幕截图中的那个。

有没有办法修复这个问题?

更新:我将包含项目的文件夹移动到我的桌面上,这样就可以正常工作了,但我不知道为什么在原始位置无法工作。感谢帮助。

英文:

So I was making a java program in Netbeans IDE 8.2 and when I tried to run it, I got this error:

  1. Error: Could not find or load main class helloworld.HelloWorld
  2. C:\Users\name\AppData\Local\NetBeans\Cache.2\executor-snippets\run.xml:53: Java returned: 1
  3. BUILD FAILED (total time: 1 second)

(Assume that helloworld is my project name and HelloWorld is my class name)

Everything was fine before, and I can't think of what I did that would cause this error to happen. This is the first time this error has happened to me so I don't know what to do. The code doesn't seem to have any errors.

Other projects of mine seemed to also have the exact same problem even though I haven't opened or edited them in weeks prior to this error occurring.

I tried many other ways but none of them seems to do the trick. Including deleting the cache folder and setting the main class through the properties tab.

Here's a screenshot of my code with the error, my name is crossed out:
Java错误:无法找到或加载主类 – Netbeans IDE
Even if I type my code directly in the main class, which is class6homework.java, the error still occurs but it says class6homework.class6homework instead of the one in the screenshot.

Any ideas on how I could fix this?

UPDATE: I got it to work by moving the folder that contains the project to my desktop, didn't know why it wouldn't work in my original location. Thanks for the help.

答案1

得分: 1

也许你没有包含主方法。如果这不是问题,尝试在项目树中右键单击你的类,然后查找一个名为“设置为主类”或类似的选项。

英文:

Perhaps you don't have the main method included. If that is not the problem, try right clicking your class in the project tree and look for an option that says "Set as main class" or something like that

答案2

得分: 0

修复方法:

修复方法 1

  1. 进入项目属性(在 Netbeans 中右键单击项目文件夹)
  2. 在显示类别的左侧选项卡中,点击“运行”选项
  3. 然后点击“浏览”以找到您项目中使用的主类

修复方法 2

  1. 前往 C:\Users\name\AppData\Local\Netbeans
  2. 删除 Cache 文件夹
  3. 重新构建并运行

修复方法 3
下载最新版本的 Netbeans

修复方法 4
下载最新版本的 JDK 并配置 Netbeans 使用该版本

英文:

Possible Fixes:

Fix 1

  1. Go to project properties (right click on the folder of your project in netbeans)
  2. On left tab where it shows the categories, click on the "Run" selection
  3. Then click on Browse to find the Main class you use on your project

Fix 2

  1. Go to C:\Users\name\AppData\Local\Netbeans
  2. delete the Cache folder.
  3. Rebuild and Run

Fix 3
Download most recent version of Netbeans

Fix 4
Download most recent version of JDK and configure Netbeans to use that

huangapple
  • 本文由 发表于 2020年8月16日 23:13:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63438495.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定