Java获取资源InvalidJarIndexException
我在同一文件夹中有一个类名 test.java 和另一个文件“log4j.properties”,但以下调用
getClass().getResource("log4j.properties")
失败
ms
Trace:
sun.misc.InvalidJarIndexException: Invalid index
at sun.misc.URLClassPath$JarLoader.getResource(Unknown Source)
at sun.misc.URLClassPath$JarLoader.getResource(Unknown Source)
at sun.misc.URLClassPath$JarLoader.findResource(Unknown Source)
at sun.misc.URLClassPath.findResource(Unknown Source)
at java.net.URLClassLoader$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findResource(Unknown Source)
at java.lang.ClassLoader.getResource(Unknown Source)
at java.lang.ClassLoader.getResource(Unknown Source)
at java.lang.Class.getResource(Unknown Source)
但 getClass().getClassLoader().getResource("log4j.properties") 有效。可能是什么原因?
I have a class names test.java and another file "log4j.properties" in the same folder but following call
getClass().getResource("log4j.properties")
fails with
ms
Trace:
sun.misc.InvalidJarIndexException: Invalid index
at sun.misc.URLClassPath$JarLoader.getResource(Unknown Source)
at sun.misc.URLClassPath$JarLoader.getResource(Unknown Source)
at sun.misc.URLClassPath$JarLoader.findResource(Unknown Source)
at sun.misc.URLClassPath.findResource(Unknown Source)
at java.net.URLClassLoader$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findResource(Unknown Source)
at java.lang.ClassLoader.getResource(Unknown Source)
at java.lang.ClassLoader.getResource(Unknown Source)
at java.lang.Class.getResource(Unknown Source)
But getClass().getClassLoader().getResource("log4j.properties") works. What can be the reason?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
InvalidJarIndexException
通常是由 JAR 中损坏的INDEX.LIST
文件引起的。因此,请检查 JAR 是否有任何问题。请参阅http://littletechsecrets.wordpress.com/2008/12 /01/why-does-invalidjarindexexception-occurr/您描述的不同结果可能与
Class.getResource()
解析相对于类包的路径,ClassLoader.getResource()
解析相对于根的路径。因此getClass().getResource("/log4j.properties")
可能会解决您的问题。The
InvalidJarIndexException
is normally caused by a corruptINDEX.LIST
file in the JAR. So check the JAR for any problems. See http://littletechsecrets.wordpress.com/2008/12/01/why-does-invalidjarindexexception-occur/The different results you describe are probably related to
Class.getResource()
resolving a path relative to the classes package, andClassLoader.getResource()
resolving a path relative to the root. SogetClass().getResource("/log4j.properties")
might fix your problem.