在 Eclipse 中重新构建已删除的类文件
我不小心删除了项目中的 .class(Java 字节码)文件(在文件系统上,而不是使用 Eclipse 本身)。很容易修复,对吧?只需重新构建它即可。但这行不通!即使我从“项目”菜单中选择“构建项目”或“构建全部”或“自动构建”,文件系统上实际上什么也没有发生,我仍然得到:
Exception in thread "main" java.lang.NoClassDefFoundError
我只想从源代码重新编译它我已经有了!
顺便说一下,当我从“Project”菜单中选择“Clean...”时,Eclipse 也不会删除任何文件。我还尝试将项目重新导入到不同的文件夹中,但 Eclipse 只是复制所有 .class 文件,问题仍然存在。
I accidentally deleted a .class (Java bytecode) file in my project (on the filesystem, not using Eclipse itself). Easy to fix, right? Just re-build it. But that doesn't work! Even if I select "Build Project" or "Build All" or "Build Automatically" from the "Project" menu, nothing actually happens on the file system, and I still get:
Exception in thread "main" java.lang.NoClassDefFoundError
I just want to re-compile this from the source code I already have!
By the way, when I choose "Clean..." from the "Project" menu, Eclipse doesn't delete any files either. I have also tried re-importing the project into a different folder, but Eclipse just copies all the .class files and the problem persists.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
OP在评论中回答了他自己的问题(两年半前):
In regards to the bounty
如果 OP 的答案没有解决您的问题,您应该在新帖子中提出更具体的问题,描述您的尝试以及 OP 的解决方案如何没有解决您的具体问题。
话虽这么说,假设您确实尝试了OP发布的解决方案,则可能是另一个问题(不是由删除类文件引起的)导致了相同的错误。因为你开始悬赏别人的问题,而你无法恢复你的声望,所以我认为有必要提及一下,可能值得你花时间来确保你的 JDK 版本在旧的或外部源代码之间兼容在你的项目中。当编译器到达代码中引用在不兼容的 JDK 上开发的库中定义的对象/类时,您将得到相同的错误
NoClassDefFoundError
,它缺少关键的内部依赖项在您的 JDK 版本中找不到。我会更详细地介绍,但由于这个问题具体是关于删除类文件引起的错误,我认为这样做是不对的。
The OP answered his own question in the comments (2 and a half years ago):
In regards to the bounty
IF the OP's answer didn't resolve your issue, you should ask a more specific question on a new post, describing what you've attempted and how the OP's solution didn't resolve your specific issue.
That being said, assuming you did try the solution the OP posted, it is possible a different issue (that wasn't caused by deleting a class file) is causing the same error. Because you started a bounty on someone else's question and you can't get your prestige back I thought it would be appropriate to mention it might be worth your time to make sure your JDK version(s) are compatible between old or external source code used in your project. You'll get the same error
NoClassDefFoundError
when the compiler reaches a point in your code that references an object/class that's defined in a library that was developed on an incompatible JDK, it's missing key internal dependencies that are not found within your JDK version.I would go into more detail, but since this question is specifically about an error that came about from deleting a class file I don't feel it's right to do so.
进行彻底清理
1) 找到并删除.eclipse文件夹(您可以先备份它们)
2) 删除相关.class文件
3) 如果有任何.class文件>.svn 文件夹,手动或通过 svn 客户端删除它们
4) 不要为此使用自动构建,而是手动选择仅损坏的项目并执行清理< /strong> (如果有依赖项)
如果那样失败,可能是打包源代码并重新导入为新项目的好主意。这可以避免在可能的 IDE bug 上浪费时间
Do a complete clean
1) Find and delete the .eclipse folder (you may back them up first)
2) Delete related .class files
3) If there are any .svn folders, delete them either manually or via your svn client
4) Do not use auto build for this, but manually select only the broken project and do a clean (in case there are dependencies)
If that fails, probably a good idea to package your source codes and re-import as a new project. That can avoid wasting time on a probable IDE bug
在更传统的语言中,程序作为启动过程的一部分一次性加载。 Java 不存在这个问题,因为它采用不同的加载方法。这是变得更容易的活动之一,因为 Java 中的一切都是对象。请记住,每个类的编译代码都存在于其自己的单独文件中。 直到需要代码时才会加载该文件。一般来说,您可以说“类代码在首次使用时加载”。这通常是在构造该类的第一个对象时发生,但在访问静态字段或静态方法时也会发生加载。
如果您无法从本地历史记录恢复。那你就不走运了。使用 SVN 或 Git 等源代码控制管理工具来避免下次出现此类意外。
如果您有源文件,请尝试在控制台或其他 IDE 中单独编译文件以及依赖项,然后复制该类文件,让错误成为错误,现在在 Eclipse 中编辑源文件,然后尝试再次构建。希望这不会起作用,因为即使 eclipse 也会忽略在构建项目时重新编译一些文件。最好尝试一下。
In more traditional languages, programs are loaded all at once as part of the startup process. Java doesn’t have this problem because it takes a different approach to loading. This is one of the activities that become easier, because everything in Java is an object. Remember that the compiled code for each class exists in its own separate file. That file isn’t loaded until the code is needed. In general, you can say that “class code is loaded at the point of first use.” This is usually when the first object of that class is constructed, but loading also occurs when a static field or static method is accessed.
If You can't restore from local history. Then you are out of Luck. Use Source Control Management Tools like SVN or Git to avoid such surprises next time.
If you are having source file try to compile file along with dependencies alone in console or other IDE and copy that class file let the errors be errors now edit source file in eclipse try to build again. Hopefully this will not work because even eclipse will neglect Re-compiling some files while building Project. Better Give a Try.
您确定该源文件位于您项目的源集中吗?因为在这种情况下 Eclipse 只会编译并将其放入类路径中。在Package Explorer中右键单击项目,Properties -> Java 构建路径 ->来源。封闭的文件夹应该在那里,否则 Eclipse 将不会编译它。
比如说,您的这个源文件曾经在源集中并且经过编译,这可以解释为什么它在您删除二进制文件之前一直在工作。
为了避免出现此问题,我建议在 Java -> 中选择清理项目时擦洗输出文件夹。编译器->项目菜单中的构建和自动构建...。
还要确保您的项目编译/构建成功,否则 Eclipse 可能无法编译所有类。
如果仍然没有帮助,那么您遇到问题的项目类型可能很重要:Java 项目、Maven 项目、Gradle 项目等。
You sure this source file is in your project's source set? Because Eclipse will only compile and put it in your classpath in that case. Right click the project in Package Explorer, Properties -> Java Build Path -> Source. The enclosing folder should be there or Eclipse won't compile it.
In case, say, this source file of yours was once in source set and was compiled that could explain why it was working up until you removed the binary.
In order for this problem not to happen I suggest having Scrub output folders when cleaning projects being selected in Java -> Compiler -> Building and Build automatically... on in Project menu.
Also make sure your project compilation/build succeeds, otherwise Eclipse may not compile all the classes.
If it still doesn't help it could be important what type of project you are having problems with: Java Project, Maven Project, Gradle Project, etc.
对于发布赏金的人来说,也许您可以将所有更改提交到您拥有的代码存储库中,退出 eclispe 后只需删除整个工作区,然后创建一个新工作区并将所有文件重新导入到新工作区中您的代码存储库。
To the person that put the bount out, maybe you could just commit all your changes to what ever code repository you have, after exiting eclispe just delete the whole workspace, then create a new workspace and re import all the files into the new workspace from your code repository.