java Netbeans 错误:无法找到或加载主类(再次)

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

Netbeans Error: Could not find or load main class (again)

javanetbeans

提问by arcy

I am very experienced in Java, but not in Netbeans. I have a project that I've been working on, a little test program for a particular issue, so I have been making changes to it and running it and debugging it many times a day, every day, for several weeks. Yesterday Netbeans quit running it the way I had been running it, and I can't seem to get it started again.

我在 Java 方面非常有经验,但在 Netbeans 方面没有经验。我有一个我一直在从事的项目,一个针对特定问题的小测试程序,所以我一直在对其进行更改并运行和调试它,每天多次,持续数周。昨天 Netbeans 停止以我一直运行的方式运行它,而且我似乎无法再次启动它。

The entire output when I run the project is:

我运行项目时的整个输出是:

run: Error: Could not find or load main class 

C:\Users\rcook\AppData\Local\NetBeans\Cache.1\executor-snippets\run.xml:53: Java returned: 1 
BUILD FAILED (total time: 0 seconds)

I note for whatever it's worth that the classname does not appear on the first line, as it seems to with others who have had the "could not find" error.

我注意到类名没有出现在第一行是值得的,因为它似乎与其他有“找不到”错误的人一样。

This happens whether I run the project (select the project and press F6) or the file (select the file and press control-F6). I have cleaned and rebuilt the project (which I didn't do often before, I've wondered if it causedthe problem). I have restarted Netbeans. I have restarted the (Win7) computer. I am not running any memory-hogging programs except for Outlook, which I have been running the entire time it was working.

无论我是运行项目(选择项目并按 F6)还是文件(选择文件并按 control-F6),都会发生这种情况。我已经清理并重建了项目(我以前不经常这样做,我想知道它是否导致了问题)。我已经重新启动了 Netbeans。我已经重新启动(Win7)计算机。我没有运行任何占用内存的程序,除了 Outlook,我一直在运行它。

I can run the program from the command line by setting my default directory down to the 'classes' directory under the project and entering java stsclientexample3.STSClientExample3.

我可以通过将默认目录设置为项目下的“classes”目录并输入java stsclientexample3.STSClientExample3.

I have checked the "Run" configuration many times -- there are no program or VM options entered, it has the correct classname. Oddly enough, if I change the classname to something that isn't there, Netbeans displays a popup that says the invalid name class wasn't found in the project, and listing the main classes available. But when I do that and select the one to run, it gives the same "could not find" error. There's one other main class in the project, if I select THAT it gives the same "could not find" error.

我已经多次检查“运行”配置——没有输入程序或 VM 选项,它具有正确的类名。奇怪的是,如果我将类名更改为不存在的名称,Netbeans 会显示一个弹出窗口,指出在项目中找不到无效的名称类,并列出可用的主要类。但是当我这样做并选择要运行的那个时,它给出了相同的“找不到”错误。项目中还有另一个主要类,如果我选择它,它会给出相同的“找不到”错误。

I have looked at the XML file referenced in the message, but it is full of ${parameter} references that I don't know the resolution of. It would likely be helpful to know how Netbeans resolves "${classname}", for instance, or even to dump the value Netbeans has at runtime somehow.

我查看了消息中引用的 XML 文件,但它充满了我不知道解析的 ${parameter} 引用。例如,了解 Netbeans 如何解析“${classname}”,或者甚至以某种方式转储 Netbeans 在运行时的值可能会很有帮助。

I have commented out code and re-run. Same result.

我已经注释掉代码并重新运行。结果一样。

I have deleted that class from the run configuration, restarted Netbeans, and added the class back. Same result.

我已从运行配置中删除了该类,重新启动了 Netbeans,然后重新添加了该类。结果一样。

By now I've read more than a dozen other queries for a similar error, on SO and other places; I've tried all the things that looked at all possible (skipping things like configuring for special-purpose libraries, etc.).

到现在为止,我已经在 SO 和其他地方阅读了十多个其他类似错误的查询;我已经尝试了所有可能的事情(跳过诸如配置特殊用途库之类的事情)。

Anyone know how to fix this?

有人知道怎么修这个东西吗?



EDIT: before someone asks, another project with a main()runs using the 'run project' option.

编辑:在有人问之前,另一个main()使用“运行项目”选项运行的项目。

EDIT2: I forgot to mention, also, that another post recommended that one delete the Netbeans cache (c:\Users\\AppData\Local\NetBeans\Cache), so I shut down NetBeans, deleted everything in that directory, and restarted NetBeans. Same result.

EDIT2:我也忘了提到另一篇文章建议删除 Netbeans 缓存 (c:\Users\\AppData\Local\NetBeans\Cache),所以我关闭了 NetBeans,删除了该目录中的所有内容,然后重新启动了 NetBeans . 结果一样。

回答by Eric G

I've encountered this problem a few times and it is very frustrating. If you delete the cache it should fix it after you rebuild the project

我遇到过几次这个问题,非常令人沮丧。如果您删除缓存,它应该在您重建项目后修复它

  1. Close netbeans
  2. Delete the Cache folder User\AppData\Local\NetBeans\Cache\(Windows 7 and above)
  3. Open Netbeans
  4. Rebuild and run
  1. 关闭 netbeans
  2. 删除缓存文件夹User\AppData\Local\NetBeans\Cache\(Windows 7 及更高版本)
  3. 打开 Netbeans
  4. 重建并运行

Edit - you can find the cache directory also by going in netbeans to Help - Aboutthen it shows your cache directory.

编辑 - 您也可以通过进入 netbeans 来找到缓存目录,Help - About然后它会显示您的缓存目录。

回答by Dmitriy S

Try creating new class with main method and copy your code into this class.

尝试使用 main 方法创建新类并将您的代码复制到这个类中。

回答by Zin Win Htet

Just close the Netbeans. Go to C:\Users\YOUR_PC_NAME\AppData\Local\Netbeansand delete the Cachefolder. The open the Netbeans again and run the project. It works like magic for me.

只需关闭 Netbeans。转到C:\Users\YOUR_PC_NAME\AppData\Local\Netbeans并删除Cache文件夹。再次打开 Netbeans 并运行该项目。它对我来说就像魔术一样。

(AppDatafolder might be hidden probably, if so, you need to make it appear in Folder Options).

AppData文件夹可能被隐藏了,如果是这样,你需要让它出现在文件夹选项中)。

enter image description here

enter image description here

回答by MadderMortem

I'm working in the Netbeans IDE 8,2. I had the same problem and in my manifest.mf, the field classpath: was empty(I noticed that because I got the message:

我在 Netbeans IDE 8,2 中工作。我遇到了同样的问题,并且在我的 中manifest.mf,字段 classpath: 是空的(我注意到这是因为我收到了消息:

java returned 1 could not load main class ExerciseFinal

java返回1无法加载主类ExciseFinal

I looked at the classpath nb where searching and I simply changed one folder's name and then after restarting netbeans everything worked.

我查看了搜索所在的类路径 nb,我只是更改了一个文件夹的名称,然后在重新启动 netbeans 后一切正常。

My file name was "teo_1=1;" and I changed it to "teo" simply because I thought that maybe 1=1 (always true) was messing the nb.

我的文件名为“teo_1=1;” 我把它改成“teo”只是因为我认为 1=1(总是正确的)可能会弄乱 nb。

回答by Master M

I had the same problem and i think the problem was in the file path, either it was too long or it had some forbidden characters(now i have seen that it was a letter from my language), when i copied a project to a different folder it worked...

我遇到了同样的问题,我认为问题出在文件路径上,要么太长,要么有一些禁止字符(现在我已经看到它是我的语言中的一个字母),当我将一个项目复制到另一个它工作的文件夹...

回答by DSlomer64

After an eternity of trying to figure it out myself, looking through every menu, submenu, tab, and option, I was still stuck. Nothing anybody suggested worked for me.

在我自己想了很久,浏览每一个菜单、子菜单、标签和选项之后,我仍然被卡住了。没有任何人建议对我有用。

How many hours did I spend flailing about in the name of pride and self-education? Several. How long did what I'm about to suggest take? Maybe 10 minutes; 15 tops.

为了自尊和自我教育,我花了多少时间挥洒汗水?一些。我要提出的建议需要多长时间?也许 10 分钟;15 顶。

Was my journey a waste? No. I learned a lot. But since I hadn't set out to learn what I did and was prevented from work I needed to do, I was pissed long before giving that up and copping out.

我的旅程是浪费吗?不,我学到了很多。但是由于我还没有开始学习我所做的事情并且被阻止从事我需要做的工作,所以在放弃和逃避之前我很生气。

So... Long story short, do this:

所以......长话短说,这样做:

(Step 1 has been deleted; renumbering steps might be bad idea. So start with 2!)

(步骤 1 已被删除;重新编号步骤可能是个坏主意。所以从 2 开始!)

Step 2: Open the problem project.

第二步:打开问题项目。

Step 3: Create a new Netbeans project, get into Project view, and expand both projects to show their packages.

第 3 步:创建一个新的 Netbeans 项目,进入项目视图,并展开两个项目以显示它们的包。

Step 4: Thoughtfullycopy all packages from problem project to new. (This means to hold off on the "main" package until last to avoid a chuteload of red-marked errors. If you see a bunch anyway, don't look at them. Further copying will fix many.)

第 4 步:仔细地将问题项目中的所有包复制到新项目中。(这意味着将“主”包推迟到最后,以避免大量红色标记的错误。无论如何,如果您看到一堆,请不要看它们。进一步复制将修复许多。)

NOTE: Having to choose a different project name for the new project will likely lead to a new name for your new "main" java class file. No problem. Just note this.

注意:必须为新项目选择不同的项目名称可能会导致新的“主”java 类文件使用新名称。没问题。请注意这一点。

Step 5: Once you've copied all but the "main" package from problem project to new, open the "main" package to see your files. Then open the new "main" package, which will have ONE tiny file in it.

第 5 步:将问题项目中除“main”包之外的所有包复制到新项目后,打开“main”包以查看您的文件。然后打开新的“主”包,其中将包含一个小文件。

Step 6: Thoughtfully copy one file at a time (holding off on the "main" class file until last) from problem project to new.

第 6 步:一次仔细地将一个文件从问题项目复制到新项目(直到最后才复制“主”类文件)。

(If you see errors, let 'em go. Not finished yet.)

(如果你看到错误,让他们走。还没有完成。)

Step 7: Once you have only the "main" class file left to copy from the problem project, open it. Also open the new "main" class file in the new project.

第 7 步:一旦您只剩下要从问题项目中复制的“主”类文件,请打开它。还要在新项目中打开新的“主”类文件。

Step 8: Copy all the code from problem project "main" java class file and paste it into the new project's "main" file.

第 8 步:复制问题项目“main”java 类文件中的所有代码并将其粘贴到新项目的“main”文件中。

Step 9. There will be errors at the end. After all, at least one package and one file name changed. So open each source file with an error, put the cursor on each line with error, and press Alt-Enter to get a hint. Renaming the new main file or changing some old package names to new will fix a few, especially in some imports; this might just get rid of the rest.

第九步,最后会有错误。毕竟,至少改变了一个包和一个文件名。所以打开每个有错误的源文件,把光标放在有错误的每一行,按Alt-Enter得到提示。重命名新的主文件或将一些旧的包名称更改为新的将修复一些问题,尤其是在某些导入中;这可能只是摆脱其余的。

I hope this helps someone. I should look for a more recent post and put this Answer there, if it gets enough upvotes.

我希望这可以帮助别人。如果得到足够的赞成,我应该寻找更新的帖子并将此答案放在那里。