Isn't it interesting to know that Java class files have a 4 byte header which could be read as CAFEBABE in Hexadecimal. This is being mentioned in Wikipedia: Java class file.
You can download the eclipse project here.
Have you ever tried printing this secret code out? If not, let's do that.
Below is the code which reads it's own class file and prints the code to output stream.
package com.javavirtues.cafebabe;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class PrintHeader {
public static void main(String[] args) {
File classFile = new File(
"bin/com/javavirtues/cafebabe/PrintHeader.class");
if (classFile.exists()) {
System.out.println("Class file exists. Reading header.");
byte[] readHeader = new byte[4];
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(classFile);
fileInputStream.read(readHeader);
for (byte readByte : readHeader) {
System.out.printf("%02X", readByte);
}
} catch (IOException exception) {
exception.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
You can download the eclipse project here.
This comment has been removed by a blog administrator.
ReplyDeleteGreat information. Couldn’t be written much better!
ReplyDeleteKeep it up!
buy logo online
This is really awesome, thanks for the share. Also, visit our website if you are looking to purchase logo:
ReplyDeletePurchase Logo