在 Eclipse 中重新构建已删除的类文件

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

Re-building a deleted class file in Eclipse

javaeclipseclassbuildcompilation

提问by user690075

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:

我不小心删除了项目中的 .class(Java 字节码)文件(在文件系统上,而不是使用 Eclipse 本身)。很容易修复,对吧?只需重新构建它。但这不起作用!即使我从“项目”菜单中选择“构建项目”或“全部构建”或“自动构建”,文件系统上实际上没有任何反应,我仍然得到:

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.

顺便说一句,当我从“项目”菜单中选择“清理...”时,Eclipse 也不会删除任何文件。我也尝试将项目重新导入到不同的文件夹中,但 Eclipse 只是复制了所有 .class 文件,问题仍然存在。

回答by Arthur Weborg

The OP answered his own question in the comments(2 and a half years ago):

OP 在评论中回答了他自己的问题(两年半前):

Found the solution: another project on which that project depended could not be compiled, because it could not be cleaned, because Eclipse wanted to delete the .svn directories throughout that project (I have no idea why), and it could not because some of the files didn't have write permission. I was happy to wipe out all the .svn data just to get this working! Thanks for the hint. – user690075 Sep 7 '11 at 1:25

找到解决方案:无法编译该项目所依赖的另一个项目,因为它无法清理,因为Eclipse想删除整个项目中的.svn目录(我不知道为什么),并且它不能因为某些这些文件没有写权限。我很高兴清除所有 .svn 数据只是为了让它工作!谢谢你的提示。– user690075 2011 年 9 月 7 日 1:25



In regards to the bounty关于赏金

This question has not received enough attention.

This problem keeps wasting hours of my time.

这个问题没有得到足够的重视。

这个问题一直在浪费我的时间。

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.

如果 OP 的回答没有解决您的问题,您应该在新帖子中提出更具体的问题,描述您的尝试以及 OP 的解决方案如何没有解决您的具体问题。

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 NoClassDefFoundErrorwhen 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.

话虽如此,假设您确实尝试了 OP 发布的解决方案,则可能是不同的问题(不是由删除类文件引起的)导致相同的错误。因为您开始悬赏别人的问题而您无法恢复声望,所以我认为值得您花时间确保您的 JDK 版本在使用的旧或外部源代码之间兼容在你的项目中。NoClassDefFoundError当编译器到达代码中引用在不兼容 JDK 上开发的库中定义的对象/类的某个点时,您将收到相同的错误,它缺少在您的 JDK 版本中找不到的关键内部依赖项。

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.

我会更详细地讨论,但由于这个问题是专门关于删除类文件引起的错误,我认为这样做是不对的。

回答by George

Do a complete clean

彻底清洁

1) Find and delete the .eclipsefolder (you may back them up first)

1)找到并删除.eclipse文件夹(可以先备份)

2) Delete related .classfiles

2)删除相关的.class文件

3) If there are any .svn folders, delete them either manually or via your svn client

3) 如果有任何.svn 文件夹,请手动或通过您的 svn 客户端删除它们

4) Do not use auto build for this, but manually select only the broken projectand do a clean(in case there are dependencies)

4)不要为此使用自动构建,而是手动选择损坏的项目并进行清理(以防存在依赖项)

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

如果失败,将源代码打包并作为新项目重新导入可能是个好主意。这可以避免在可能的 IDE 错误上浪费时间

回答by GreenJava

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.

在更传统的语言中,程序是作为启动过程的一部分一次性加载的。Java 没有这个问题,因为它采用不同的加载方法。这是变得更容易的活动之一,因为 Java 中的一切都是对象。请记住,每个类的编译代码都存在于其自己的单独文件中。在需要代码之前不会加载该文件。通常,您可以说“在第一次使用时加载类代码”。这通常是在构造该类的第一个对象时,但在访问静态字段或静态方法时也会发生加载。

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.

如果您无法从本地历史记录中恢复。那你运气不好。使用 SVN 或 Git 等源代码控制管理工具避免下次出现此类意外。

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.

如果您有源文件,请尝试在控制台或其他 IDE 中单独编译文件和依赖项,然后复制该类文件,让错误成为错误,现在在 eclipse 中编辑源文件尝试再次构建。希望这不会起作用,因为即使是 eclipse 也会在构建项目时忽略重新编译一些文件。最好试一试。

回答by aljipa

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.

你确定这个源文件在你项目的源集中?因为在这种情况下 Eclipse 只会编译并将其放入您的类路径中。右键单击Package Explorer 中的项目,Properties -> Java Build Path -> Source。封闭文件夹应该在那里,否则 Eclipse 不会编译它。

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 projectsbeing selected in Java -> Compiler -> Buildingand Build automatically...on in Project menu.

为了不发生这个问题,我建议在清理项目时使用Scrub 输出文件夹,Java -> 编译器 ->自动构建构建...在项目菜单上。

Also make sure your project compilation/build succeeds, otherwise Eclipse may not compile all the classes.

还要确保您的项目编译/构建成功,否则 Eclipse 可能无法编译所有类。

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.

如果它仍然没有帮助,那么您遇到问题的项目类型可能很重要:Java 项目、Maven 项目、Gradle 项目等。

回答by mpop

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.

对于提出奖励的人,也许您可​​以将所有更改提交到您拥有的任何代码存储库,退出 eclispe 后只需删除整个工作区,然后创建一个新工作区并将所有文件重新导入到新工作区中你的代码库。