eclipse 将 Java 项目导出为 .jar 文件会抱怨找不到某些 .java 文件

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

Exporting Java project as .jar file will complain that certain .java files aren't found

javaeclipse

提问by devoured elysium

When trying to export a project as a .jarfile, Eclipse will complain that some files can't be found on the classpath:

当尝试将项目导出为.jar文件时,Eclipse 会抱怨在类路径中找不到某些文件:

Class files on classpath not found or not accessible for: 'proj/src/main/java/analysis/specification/VerifyVariableIsDefinedInsideTargetTerm.java'

找不到或无法访问类路径上的类文件:'proj/src/main/java/analysis/specification/VerifyVariableIsDefinedInsideTargetTerm.java'

Now, the interesting part is that these files haven't actually existed on the disk for ages. I guess I force Eclipse to refresh its database some way. I've tried doing F5 for a full build, but it seems to no avail. I've also tried the procedure described in this blog post, but the issue remains.

现在,有趣的部分是这些文件实际上已经存在于磁盘上很久了。我想我会强制 Eclipse 以某种方式刷新其数据库。我试过用 F5 进行完整构建,但似乎无济于事。我也尝试过此博客文章中描述的过程,但问题仍然存在。

回答by devoured elysium

Error… I actually tripped on the reason for the problem a few moments ago…

错误……我刚才真的被问题的原因绊倒了……

All the files that were raising problems did actually exist on the project but were commented out (and thus, I didn't see them in the package explorer which is the view I generally use).

所有引发问题的文件确实存在于项目中,但被注释掉了(因此,我没有在包浏览器中看到它们,这是我通常使用的视图)。

After deleting them, everything went fine. I still don't understand why would they raise errors in the JAR creation process, though…

删除它们后,一切顺利。我仍然不明白为什么他们会在 JAR 创建过程中引发错误,但是......

EDIT:Here's an example of a commented out class:

编辑:这是一个注释掉的类的例子:

package a.b.c

//import java.io.Closeable;

//public class Example implements Closeable {
//    public void close() throws IOException {
//        System.out.println("closed");
//    }
//}

Because the whole class is commented out, no Example.classis created. Whereas if the class is commented out like this:

因为整个类都被注释掉了,所以没有Example.class创建。而如果类被这样注释掉:

package a.b.c

//import java.io.Closeable;

public class Example
//    implements Closeable
{
//    public void close() throws IOException {
//        System.out.println("closed");
//    }
}

an Example.classis created, although empty.

一个Example.class被创建,虽然是空的。

This may be a bug in eclipse. If you export the ant build script from eclipse and use that, it creates the JAR just fine.

这可能是 eclipse 中的一个错误。如果您从 Eclipse 导出 ant 构建脚本并使用它,它会很好地创建 JAR。

回答by Nirav Radia

This issue occurred to me also. However, the problem that I had was that I had JRE configuration issues in Eclipse.

这个问题也出现在我身上。但是,我遇到的问题是我在 Eclipse 中遇到了 JRE 配置问题。

回答by Aerophite

Probably the real problem is that not all your class files are in the same place… My suggestion is to move all your .javafiles into the same folder, recompile to make the .classfiles, make sure all the .classfiles are then in the same folder… Then, check out this webpage, if you need any help on actually creating the .jar.

可能真正的问题是不是所有的类文件都在同一个地方……我的建议是将所有.java文件移动到同一个文件夹中,重新编译以制作.class文件,确保所有.class文件都在同一个文件夹中……然后,查看此网页,如果您在实际创建.jar.

The website also assumes you are on a Mac, but you should still be able to use a command prompt on Windows to get it work.

该网站还假设您使用的是 Mac,但您仍然应该能够在 Windows 上使用命令提示符来使其工作。