java.lang.ClassFormatError: Extra bytes at end of class file

To clarify: this happens after you've cleaned out all old .class files and recompiled on the same machine?

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).

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.


This is indeed disallowed as per VM Spec 4.9.1:

The class file must not be truncated or have extra bytes at the end.

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 always with older runtime versions. Check the versions using java -version and 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.

Another possible cause is a hardware error, e.g. corrupt harddisk/file/memory. Try recompiling or another machine.

Tags:

Java

Exception