How do I find the magic number 0xCAFEBABE?

I've heard that Java classes have something called a magic number in the headers that identifies them, and that says something like 0xCAFEBABE.

Could anyone explain to me how I can check such information?

 11
Author: BetaM, 2015-12-02

2 answers

Good about your question, when you open a file .class with a hex editor, you can observe "cafe babe" if it contains this magic number.

In fact here I found an explanation of own James Gosling

We used to go for lunch to a place called St Michaels Alley. Of according to a local legend, in the deep and dark past, the group Grateful Dead used to play there, before they became famous. Era a rather modern place that honored the name of the band (a worthy place to die). When Jerry The leader of the band died they placed a small Buddhist altar. When we went there we used to call to the Place "Cafe Dead". Someone noticed it was a hexadecimal number. I I was re-provisioning the code of the class file format and needed some magic numbers: one for the persistent object file and another for classes. I use CAFEDEAD for object file persistent, and looking for a pattern in the first 4 digits "CAFE", and a variant on the others came to mind BABE and use it. In that moment did not seem too important or destined to go to any side like the trash can of history. So CAFEBABE got to be the class file format and CAFEDEAD the class file format. persistent object, but over time the persistent object left along with the use of CAFEDEAD they were rescheduled by RMI."

enter the description of the image here

The first 4 bytes are "the magic number",

  • 0xcafebabe,

To identify a valid class file.

The next 2 bytes minor version number identify the version of the.class that is used.

The next 2 bytes major version number

  • J2SE 8 = 52 (0x34 hex),
  • J2SE 7 = 51 (0x33 hex),
  • J2SE 6.0 = 50 (0x32 hex),
  • J2SE 5.0 = 49 (0x31 hex),
  • JDK 1.4 = 48 (0x30 hex),
  • JDK 1.3 = 47 (0x2f hex),
  • JDK 1.2 = 46 (0x2e hex),
  • JDK 1.1 = 45 (0x2d hex).
 15
Author: Jorgesys, 2015-12-06 02:18:01

The magic number is present at the start of all files .class as a result of the source code compilation process. With a simple text editor like Notepad in Windows it is possible to observe this, or with any hexadecimal text editor, like Notepad++, also opening the files with a common editor present in GNU/Linux operating systems omo gedit, nano or vi.

 3
Author: dwarandae, 2015-12-02 17:18:30