Parse the Java class file format-parse the magic number and version number

Source: Internet
Author: User
I have been familiar with the Java language for several years, and I have learned a little about the technology. Now I am mainly engaged in the work on the technical aspects of j2s. I have not been very busy recently, I found this ebook, deep into the Java Virtual Machine (version 2). I read it carefully and it is hard to read it. Now I share the Reading Notes in the Java class file format with you. As we all know, Java executable files are class files, commonly known as class files. This file specifies the file format in detail so that virtual machines on different platforms can correctly interpret the file. The following is an introduction in sequence: 1. magic to help the Virtual Machine identify whether a file is a class file, sun stipulates that every class file must start with a word (4 bytes). This number is called the magic number. The magic number consists of four byte unsigned numbers, and the magic number must be 0 xcafebabe. 2. The subsequent content of the version number is the length of a word to indicate the version number of the generated class file. The version number is divided into the major version number and the minor version number, the first two bytes indicate the minor version number, and the last two bytes indicate the major version number. The order of the Primary and Secondary versions follows the principle that the primary and secondary versions are sorted in descending order. Below I wrote a very simple example code, which reads the information from a file named first. class and then outputs it. Please let us know if there are any shortcomings! The code of this program is as follows: Import Java. io. *;/*** parse the class file format */public class parseclassfile {public static void main (string ARGs []) {try {// read the file data, the file is first in the current directory. class fileinputstream FCM = new fileinputstream (". /First. class "); int length = fiis. available (); // file Data byte [] DATA = new byte [length]; // read the file to the byte array. read (data); // close the file. close (); // parse the file data parsefile (data);} catch (exception e) {system. out. println (e) ;}} Private Static void parsefile (byte [] data) {// output magic number system. out. print ("magic (MAGIC): 0x"); system. out. print (integer. tohexstring (data [0]). substring (6 ). touppercase (); system. out. print (integer. tohexstring (data [1]). substring (6 ). touppercase (); system. out. print (integer. tohexstring (data [2]). substring (6 ). touppercase (); system. out. println (integer. tohexstring (data [3]). substring (6 ). touppercase (); // main version number and next version number int minor_version = (INT) data [4]) <8) + data [5]; int major_version = (INT) data [6]) <8) + data [7]; system. out. println ("version):" + major_version + ". "+ minor_version );}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.