简体   繁体   English

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

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

I am very experienced in Java, but not in Netbeans.我在 Java 方面非常有经验,但在 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.昨天 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\8.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).无论我运行项目(选择项目并按 F6)还是文件(选择文件并按 control-F6),都会发生这种情况。 I have cleaned and rebuilt the project (which I didn't do often before, I've wondered if it caused the problem).我已经清理并重建了项目(我以前不经常这样做,我想知道它是否导致了问题)。 I have restarted Netbeans.我已经重新启动了 Netbeans。 I have restarted the (Win7) computer.我已经重新启动了(Win7)电脑。 I am not running any memory-hogging programs except for Outlook, which I have been running the entire time it was working.除了 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 .我可以通过将默认目录设置为项目下的“类”目录并输入java stsclientexample3.STSClientExample3从命令行运行该程序。

I have checked the "Run" configuration many times -- there are no program or VM options entered, it has the correct classname.我已经多次检查“运行”配置——没有输入任何程序或 VM 选项,它具有正确的类名。 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.奇怪的是,如果我将类名更改为不存在的名称,Netbeans 会显示一个弹出窗口,提示在项目中未找到无效名称类,并列出可用的主要类。 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.项目中还有另一个主类,如果我选择它,它会给出相同的“找不到”错误。

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.我查看了消息中引用的 XML 文件,但它充满了我不知道其解析度的 ${parameter} 引用。 It would likely be helpful to know how Netbeans resolves "${classname}", for instance, or even to dump the value Netbeans has at runtime somehow.例如,了解 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.我已经从运行配置中删除了该类,重新启动了 Netbeans,然后又添加了该类。 Same result.同样的结果。

By now I've read more than a dozen other queries for a similar error, on SO and other places;到目前为止,我已经在 SO 和其他地方阅读了十几个其他类似错误的查询; I've tried all the things that looked at all possible (skipping things like configuring for special-purpose libraries, etc.).我已经尝试了所有可能的事情(跳过诸如配置专用库等的事情)。

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

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

Edit - you can find the cache directory also by going in netbeans to Help - About then it shows your cache directory.编辑 - 您也可以通过进入 netbeans Help - About然后它会显示您的缓存目录。

Try creating new class with main method and copy your code into this class.尝试使用 main 方法创建新类并将您的代码复制到此类中。

Just close the Netbeans.只需关闭 Netbeans。 Go to C:\Users\YOUR_PC_NAME\AppData\Local\Netbeans and delete the Cache folder.转到C:\Users\YOUR_PC_NAME\AppData\Local\Netbeans并删除Cache文件夹。 The open the Netbeans again and run the project.再次打开 Netbeans 并运行该项目。 It works like magic for me.它对我来说就像魔术一样。

( AppData folder might be hidden probably, if so, you need to make it appear in Folder Options). AppData文件夹可能被隐藏了,如果是这样,你需要让它出现在文件夹选项中)。

在此处输入图像描述

I'm working in the Netbeans IDE 8,2.我在 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:我遇到了同样的问题,在我的manifest.mf中,字段 classpath: 是空的(我注意到因为我收到了消息:

java returned 1 could not load main class ExerciseFinal java 返回 1 无法加载主类 ExerciseFinal

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;"我的文件名为“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(始终为真)弄乱了 nb。

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...我有同样的问题,我认为问题出在文件路径上,要么太长,要么有一些禁止使用的字符(现在我看到这是我的语言中的字母),当我将一个项目复制到另一个文件夹它工作...

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;也许10分钟; 15 tops. 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: Thoughtfully copy all packages from problem project to new.第 4 步:仔细地将所有包从问题项目复制到新项目。 (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.) (这意味着将“主”包推迟到最后,以避免大量红色标记的错误。如果你无论如何看到一堆,不要看它们。进一步复制会修复很多。)

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.注意:必须为新项目选择不同的项目名称可能会导致新“主”java 类文件的新名称。 No problem.没问题。 Just note this.请注意这一点。

Step 5: Once you've copied all but the "main" package from problem project to new, open the "main" package to see your files.第 5 步:将问题项目中除“main”包以外的所有包复制到新项目后,打开“main”包以查看文件。 Then open the new "main" package, which will have ONE tiny file in it.然后打开新的“主”包,里面会有一个小文件。

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.第 7 步:一旦您只剩下要从问题项目中复制的“主”类文件,请打开它。 Also open the new "main" class file in the new project.同时在新项目中打开新的“主”类文件。

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. Step 9. 最后会出现错误。 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.因此,打开每个有错误的源文件,将光标放在有错误的每一行上,然后按 Alt-Enter 以获得提示。 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.这可能只是摆脱其余部分。

I hope this helps someone.我希望这可以帮助别人。 I should look for a more recent post and put this Answer there, if it gets enough upvotes.如果获得足够的支持,我应该寻找更新的帖子并将此答案放在那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM