java.lang.ClassFormatError:类文件末尾的额外字节
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3045422/
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
java.lang.ClassFormatError: Extra bytes at end of class file
提问by CheesePls
I'm getting an odd error when I try and run this program. The class compiles fine into multiple .class files and I compiled it last week (before editing it) just fine. But now, I see this:
当我尝试运行这个程序时遇到一个奇怪的错误。该类可以很好地编译成多个 .class 文件,我上周(在编辑它之前)编译得很好。但是现在,我看到了这个:
Exception in thread "main" java.lang.ClassFormatError: Extra bytes at the end of class file blah/hooplah/fubar/nonsense/IndexId$Transaction
From what I've looked up, Java 6 build 1.5 could fix it since it allows extra bytes at the end of class files (I think), but I would much rather use build 1.6.
从我查过的内容来看,Java 6 build 1.5 可以修复它,因为它允许在类文件的末尾有额外的字节(我认为),但我更愿意使用 build 1.6。
I'm editing on Windows and then FTP-ing the .java files over to an OpenVMS machine where I then compile them. after compiling I move the .class file into a directory created from exploding the previous jar file and then re-jar.
我在 Windows 上进行编辑,然后将 .java 文件通过 FTP 传输到 OpenVMS 机器上,然后在那里编译它们。编译后,我将 .class 文件移动到一个目录中,该目录是通过分解先前的 jar 文件然后重新 jar 创建的。
Any clear ideas on how this happened or how to fix it?
关于这是如何发生的或如何解决的任何明确的想法?
采纳答案by CheesePls
The problem was solved by removing all Line Feeds from the .java file and properly renaming it(OpenVMS defaults to all lower case unless told not to)
该问题已通过从 .java 文件中删除所有换行符并正确重命名来解决(除非被告知不这样做,否则 OpenVMS 默认为所有小写)
Sadly a failure on my part by not testing between each but at least it works.
可悲的是,我没有在每个人之间进行测试,但至少它有效,这是我的失败。
In short:
简而言之:
-Line Feeds are bad ANDName files properly (Java standards not OS standards)
-Line饲料是坏的和名称的文件正确(Java标准不OS标准)
回答by BalusC
This is indeed disallowed as per VM Spec 4.9.1:
根据VM 规范 4.9.1确实不允许这样做:
The
class
file must not be truncated or have extra bytes at the end.
该
class
文件不能被截断,或者在年底额外的字节。
This can occur if there's an incompatibility in Java compiler and Java runtime used. Verify both versions and make sure that you compile for the right runtime versions. I.e. the compiled class can be used with same or newer runtime version, but not alwayswith older runtime versions. Check the versions using java -version
and javac -version
.
如果使用的 Java 编译器和 Java 运行时不兼容,就会发生这种情况。验证两个版本并确保为正确的运行时版本进行编译。即编译后的类可以与相同或更新的运行时版本一起使用,但并不总是与旧的运行时版本一起使用。使用java -version
和检查版本javac -version
。
Another common cause is that the file get corrupted during file transfer (FTP) between different machines. This transfer should be done in binary mode rather than text mode.
另一个常见原因是文件在不同机器之间的文件传输 (FTP) 期间损坏。这种传输应该在二进制模式而不是文本模式下完成。
Another possible cause is a hardware error, e.g. corrupt harddisk/file/memory. Try recompiling or another machine.
另一个可能的原因是硬件错误,例如损坏的硬盘/文件/内存。尝试重新编译或其他机器。
回答by Anon
To clarify: this happens after you've cleaned out all old .class files and recompiled on the same machine?
澄清一下:这是在您清除所有旧的 .class 文件并在同一台机器上重新编译之后发生的吗?
Or are you compiling on one machine and then copying the files to another? If that's the case, then it's likely that your file transfer software is corrupting the files (Windows <-> Linux is a common culprit, most often by adding/removing a 0x0D byte, but occasionally by adding a 0x1A DOS EOF marker).
或者您是在一台机器上编译,然后将文件复制到另一台机器上?如果是这种情况,那么很可能是您的文件传输软件损坏了文件(Windows <-> Linux 是一个常见的罪魁祸首,最常见的是添加/删除 0x0D 字节,但偶尔会添加 0x1A DOS EOF 标记)。
I suspect that if you check your process, you'll find that somewhere you're modifying the files outside of Java. There's no reason -- even version changes -- for a file produced by a valid Java compiler to have extra bytes at the end.
我怀疑如果你检查你的过程,你会发现在某个地方你正在修改 Java 之外的文件。由有效的 Java 编译器生成的文件没有理由——甚至版本更改——在末尾有额外的字节。
回答by MaxNevermind
I had similar problem. I just tried to write one class on my office PC and transfer to our client server to test something< because there were not JDK on that machine. I used the same version of java on both machines but after a transfer I got that Exception. I tried to use archiver before transferring and it helped.
我有类似的问题。我只是尝试在我的办公室 PC 上编写一个类并传输到我们的客户端服务器来测试一些东西< 因为那台机器上没有 JDK。我在两台机器上使用了相同版本的 java,但在传输后我得到了那个异常。我在传输之前尝试使用存档器,它有所帮助。
回答by Gernot
I have encountered that exception during development only. It seems to me that Eclipse's ECJ (Eclipse Luna) induces that behaviour. For me a clean build solved the issue.
我仅在开发过程中遇到过该异常。在我看来,Eclipse 的 ECJ(Eclipse Luna)诱发了这种行为。对我来说,一个干净的构建解决了这个问题。