Adam Bien's Weblog
What are the first four bytes of the .class file?
In the time of JDK 1.0/1.1 the was a long debate about different IDEs like Visual Cafe, JBuilder, Sun's Java Workshop, CodeWarrior etc. It was very similar to the today discussions about Ruby, .NET and Java :-). In every new project I was asked first about the different IDEs their advantages and drawbacks.
Once I was bored about the, not always constructive, discussions and said: "Because of the long compilation time, I write straight the byte code with UltraEdit in hex-mode".
You have just to remember the first bytes: "CAFEBABE" - everything else is easy.
No one expected such an answer - no one laughed. After two weeks the project managed came to me and asked my for a trainig about "Direct byte code programming" - he expected to save money on IDE-licenses :-). I was of course not able to give such a training and explained that...
But it is true: the first four bytes of every class-file are "CAFEBABE". If you change a letter - you will get a ClassFormatError - just try it out! It is actually waste of space - but cool :-)
Posted at 11:33AM Aug 13, 2006 by Adam Bien in Java / JEE / Architecture | Comments[3] | Views/Hits: --
*NEW* Workshop: "Real World Java EE 6/7 Bootstrap" and book: Real World Java EE Night Hacks--Dissecting the Business Tier Tweet Follow @AdamBien



CAFEBABE is four bytes. That's eight nibbles.
Posted by Brian Duff on August 13, 2006 at 01:08 PM CEST #
Yes - you are right. I will change the post NOW :-)
Thank you!
Posted by Adam Bien on August 13, 2006 at 03:40 PM CEST #
I'm not sure I agree it's really a waste of space - it's a quick and easy way to make 80% sure the classloader is really looking at a .class file, not some text or other binary format.
See this discussion
http://en.wikipedia.org/wiki/File_format#Magic_number
for more information on magic numbers ...
Posted by Stefan Tilkov on August 13, 2006 at 04:27 PM CEST #