我的代码在 Eclipse 中编译,但我在运行时得到 ClassNotFound 我该如何修复它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/764614/
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
My code compiles in Eclipse but I get a ClassNotFound at runtime how can I fix it?
提问by Michael Jay
What would cause a jar to be added successfully to an Eclipse Java project but then at runtime some of the classes from that jar are not found with ClassNotFound exceptions?
什么会导致 jar 成功添加到 Eclipse Java 项目,但在运行时却找不到 ClassNotFound 异常中的某些类?
回答by ojblass
The CLASSPATH variable at runtime is likely set incorrectly or you missed adding some classes to your jar file.
运行时的 CLASSPATH 变量可能设置不正确,或者您错过了向 jar 文件添加一些类。
回答by Charlie Martin
@ojblass almost certainly has it right. Just to extend it, you can dump the jar file with the jar(1)tool and see if the class is there. I've had surprises from Eclipse where the project definition didn't understand what was to go into a jar file I was generating to run with java -jar
.
@ojblass 几乎可以肯定它是对的。只是为了扩展它,您可以使用jar(1)工具转储 jar 文件,然后查看该类是否存在。我从 Eclipse 中得到了惊喜,其中项目定义不明白要进入我生成的 jar 文件以使用java -jar
.
回答by John Ellinwood
Everybody seems to be on the right track. Why don't you, while you have the jar unzipped, add the entire thing as a source folder in your eclipse project to see if you can even load it that way? Sometimes, depending on the classloader, you can get the CNFE if your class is available multiple times on the classpath... the classloader just bails because it doesn't know which one to use.
每个人似乎都在正确的轨道上。为什么不在解压 jar 的同时将整个内容作为源文件夹添加到 eclipse 项目中,以查看是否可以以这种方式加载它?有时,根据类加载器,如果您的类在类路径上多次可用,您可以获得 CNFE……类加载器只是因为不知道使用哪个而退出。
回答by Hunter-Orionntheitroad
I had this error occur up for having a missing permission. Turns out checking to see if you are connected to the internet (i.e., reachability) and permission to use the internet are separate permissions (live and learn.)
我因缺少权限而出现此错误。事实证明,检查您是否已连接到互联网(即可达性)和使用互联网的权限是单独的权限(生活和学习)。
The missing permission caused the app to fail with ClassNotFound error when it tried to run a reachability test.
缺少权限导致应用程序在尝试运行可达性测试时失败并显示 ClassNotFound 错误。
Hope that helps someone because this was not a fun time sink to work through.
希望对某人有所帮助,因为这不是一个有趣的时间槽。