尝试运行 Java jar 文件时出现“ClassFormatError: Incompatible magic value”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6797296/
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
"ClassFormatError: Incompatible magic value" when trying to run Java jar file
提问by user858819
I typed in "java -jar ShowTime.jar", and got this error message:
我输入了“java -jar ShowTime.jar”,得到了这个错误信息:
Exception in thread "main" java.lang.ClassFormatError: Incompatible magic value 1347093252 in class file ShowTime
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access##代码##0(URLClassLoader.java:58)
at java.net.URLClassLoader.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
How should I fix this? P.S. I have a mac.
我应该如何解决这个问题?PS我有一台Mac。
回答by fvu
A Java class should start with the magic (hex) value 0xCAFEBABE
(neat huh). Your value 1347093252
is 0x504B0304
in hex, which happens to be the magic value for a ZIP file (first 2 bytes in ASCII are PK for Phil Katz, creator of the ZIP format). A jar is also a zipfile btw, so probably your jar is pretty much corrupt. Try rebuilding the entire project.
Java 类应该以魔法(十六进制)值开头0xCAFEBABE
(不错吧)。您的值1347093252
是0x504B0304
十六进制的,这恰好是 ZIP 文件的魔法值(ASCII 中的前 2 个字节是ZIP 格式的创建者Phil Katz 的PK )。一个 jar 也是一个 zipfile btw,所以你的 jar 可能已经损坏了。尝试重建整个项目。
回答by Jon7
That usually means that you compiled the jar for a newer version of java than you ran it with. Check to see if you are using the same version of java to compile and run. If that doesn't fix the problem, please provide more info such as the compiler command and the output of java -version
.
这通常意味着您为比运行它的 Java 版本更新的 Java 版本编译了 jar。检查你是否使用相同版本的java来编译和运行。如果这不能解决问题,请提供更多信息,例如编译器命令和java -version
.
回答by Prabin
It means your application is using a jar file which is complied in a specific format in some other system. Now when you are trying to use the project in other system, the other system is not able to decode the jar, as each system generated a specific key to bundle the project.
这意味着您的应用程序正在使用在其他系统中以特定格式编译的 jar 文件。现在,当您尝试在其他系统中使用该项目时,其他系统无法对 jar 进行解码,因为每个系统都生成了一个特定的密钥来捆绑该项目。
Remote debug id the best way to find the issue that which jar is not in the proper format. Run the command in comment prompt for remote debug
远程调试 id 是查找哪个 jar 格式不正确的问题的最佳方法。在注释提示中运行命令进行远程调试
java -agentlib:jdwp=transport=dt_socket,server=y,address=8001,suspend=n (command and parameter to run you jar)
java -agentlib:jdwp=transport=dt_socket,server=y,address=8001,suspend=n(运行jar的命令和参数)
what you need is, find the jar file and replace with another jar (may be a freshly downloaded one or if you teammate has the jar. Use it.)
你需要的是,找到这个 jar 文件并用另一个 jar 替换(可能是一个新下载的,或者如果你的队友有这个 jar。使用它。)
The error i was getting because my project way having dependency with another project ABC, and ABC was using a generic jar. But project ABC was bundled with the magic key. I have replaced the jar used by ABC with a downloaded jar.
我得到的错误是因为我的项目方式依赖于另一个项目 ABC,而 ABC 使用的是通用 jar。但是 ABC 项目与魔法钥匙捆绑在一起。我已经用下载的 jar 替换了 ABC 使用的 jar。