FatalErrors
Home Blogs

Learn about JVM - know about JVM and bytecode file

Mind map of this article

1, Brief introduction to JVM

1.1 what is a JVM?

JVM (java virtual machine), java virtual machine, is a fictitious computer, but it has its own perfect hardware structure: processor, stack, register, etc. java virtual machine is used to execute bytecode files.

1.2 why can Java cross platform?

First of all, we can ask such a question, why can't C language cross platform? As shown in the figure below:

The corresponding compiler of C language on different platforms will compile it into different machine code files, and different machine code files can only run in this platform.

The execution process of java file is as follows:

java compiles the source file into a. class file (bytecode file) through javac. The bytecode file follows the specification of the JVM, which makes it run under the JVMs of different systems.

Summary

  • java code is not executed directly on the computer, but in the JVM. Different operating systems have different JVMs, but provide the same interface.
  • javac will first compile the. java file into a binary bytecode file. The bytecode file is independent of the operating system platform and only faces the JVM. Note that the bytecode file of the same code segment is the same.
  • Then the JVM executes the bytecode file, and JVMs under different operating systems will map the same bytecode file to API calls of different systems.
  • The JVM is not cross platform, java is cross platform.

1.3 why does the JVM cross language

As mentioned earlier, "the. class file is a bytecode file that complies with the JVM specification". It is not hard to think that as long as another language also follows the JVM specification and can compile its source file into a. class file, it can also run on the JVM. As shown in the figure below:

1.4 relationship among JDK, JRE and JVM

Let's take a look at the official picture:

Definition of the three

  • JDK: JDK(Java SE Development Kit), a Java standard development kit, provides various tools and resources for compiling and running Java programs, including java compiler (javac), Java runtime environment (JRE), and common Java class libraries.
  • JRE: JRE (Java runtime environment), Java runtime environment, which is used to interpret bytecode files executing Java. Ordinary users only need to install JRE to run Java programs. And the program developer must install JDK to compile and debug the program.
  • JVM: the JVM (java virtual mechanical), which is part of the JRE. It is responsible for interpreting and executing bytecode files. It is a virtual computer that can run java bytecode files.

Differences and connections

  1. JDK is used for development, and JRE is used to run Java programs. If you only run Java programs, you can install JRE without installing JDK.
  2. JDK contains JRE, and both JDK and JRE contain JVM.
  3. JVM is the core of java programming language and has platform independence.

2, Detailed explanation of bytecode file

Official document address: https://docs.oracle.com/javase/specs/jvms/se11/html/jvms-4.html#jvms-4.1

2.1 structure of bytecode file

ClassFile {
    u4             magic;
    u2             minor_version;
    u2             major_version;
    u2             constant_pool_count;
    cp_info        constant_pool[constant_pool_count-1];
    u2             access_flags;
    u2             this_class;
    u2             super_class;
    u2             interfaces_count;
    u2             interfaces[interfaces_count];
    u2             fields_count;
    field_info     fields[fields_count];
    u2             methods_count;
    method_info    methods[methods_count];
    u2             attributes_count;
    attribute_info attributes[attributes_count];
}
  • "u4, u2" in "ClassFile" refers to the length of each item of data, u4 represents 4 bytes, u2 represents 2 bytes, and so on.
  • One 16 bit file can be represented as a 2-bit 16-bit binary system. with Main.class The file's beginning cafe is analyzed as an example

    Therefore, u4 corresponds to four bytes, which is cafe babe

Next, the structure of ClassFile is analyzed

  1. magic
    In the first four bytes of the class file, the magic number of the class file is stored. This magic number is the mark of the class file and is a fixed value: 0xcafebabe. That is to say, it is the standard to judge whether a file is in class format. If the first four bytes are not 0xcafebabe, it means that it is not a class file and cannot be recognized by the JVM.
  2. minor_version and major_version
    The minor version number and major version number determine the version of the class file file. If major_ M, minor_ If version is marked as m, the version number of the file is M.m. Therefore, you can sort the versions of the class file format in dictionary order, for example, 1.5 < 2.0 < 2.1. If and only if V is in a continuous range of Mi.0 ≤ V ≤ Mj.m, the Java virtual machine implementation can support the class file format of version v. The scope list is as follows:
  3. constant_pool_count
    constant_pool_ The value of the count entry is equal to constant_ Add 1 to the number of entries in the pool table. If constant_ The pool index is greater than zero and less than constant_pool_count, the index is considered valid, but CONSTANT_Long_info and constant_ Double_ Except for constants of type info.
  4. constant_pool
    constant_pool is a structure table that represents various string constants, class and interface names, field names, and other constants referenced in the ClassFile structure and its substructures. Each constant_ The format of the pool table entry is indicated by its first "label" byte. constant_ The index of the pool table is from 1 to constant_pool_count-1.
    Java virtual machine instructions do not depend on the runtime layout of classes, interfaces, class instances, or arrays. Instead, the instruction refers to constant_ Symbol information in the pool table.
    All constant_pool table entries have the following general format:
cp_info {
    u1 tag;
    u1 info[];
}

constant_ Each entry in the pool table must begin with a 1-byte label indicating the type of constant that the entry represents. There are 17 kinds of constants, which are listed in the following table with corresponding marks. Each label byte must be followed by two or more bytes to provide information about a specific constant. The format of the additional information depends on the tag byte, that is, the contents of the info array vary with the value of the tag.

  1. access_flags
    access_ The value of the flags entry is the mask of the flag that represents access and properties to the class or interface. When set, the interpretation of each flag is specified in the following table.

  2. this_class
    this_ The value of the class entry must point to constant_ Valid index of the pool table. Constant at the index_ The pool entry must be a constant that represents the class or interface defined by such a file_ Class_ Info structure.

    //Used to represent a class or interface
    CONSTANT_Class_info {
    u1 tag;
    u2 name_index;
    }
    
  3. super_class
    For a class, the value of the parent class index must be zero or constant_ A valid index in the pool table. If super_ If the value of the class entry is non-zero, the constant at the index_ Pool entry must be CONSTANT_Class_info structure, which represents a direct superclass of the class defined by such a file. A direct superclass or any of its superclasses cannot be in the access of its ClassFile structure_ Set ACC in flags_ Final logo. If super_ If the value of the class entry is zero, the class can only be java.lang.Object This is the only class or interface that does not have a direct superclass. For interfaces, the value of the parent index must always be constant_ A valid index in the pool table. Constant at the index_ Pool entry must be java.lang.Object Constant of_ Class_ Info structure.

  4. interfaces_count
    interfaces_ The value of the count entry gives the number of direct hyperinterfaces for this class or interface type.

  5. interfaces[]
    Each value of the interface table must be constant_ A valid index in the pool table. Each value of interfaces [i] (where 0 ≤ I < interfaces_ Count)_ Pool entry must be CONSTANT_Class_info structure, which describes the direct hyperinterface of the current class or interface type.

  6. fields_count
    The value of the field counter gives the field in the fields table_ Number of info structures. field_ The info structure represents all fields declared by this class or interface type, including class variables and instance variables.

  7. fields[]
    Each value in the field table must be a field_info structure to provide a complete description of the fields in the class or interface. The field table contains only fields declared by this class or interface, not fields inherited from a superclass or interface.
    The field structure is as follows:

    field_info {
        u2             access_flags;
        u2             name_index;
        u2             descriptor_index;
        u2             attributes_count;
        attribute_info attributes[attributes_count];
    }
    
  8. methods_count
    The value of the method counter represents the method in the method table_ Number of info structures.

  9. methods[]
    Each value in the method table must be a method_info structure to provide a complete description of the methods in the class or interface. If in method_ Access of info structure_ ACC is not set in flags_ Native and ACC_ The Java virtual machine instruction to implement the method will also be provided with the abort flag;
    method_ The info structure represents all methods declared by this class or interface type, including instance methods, class methods, instance initialization methods, and methods initialized by any class or interface. The method table does not contain a representation of methods inherited from a superclass or a superinterface.
    The method has the following structure

    method_info {
        u2             access_flags;
        u2             name_index;
        u2             descriptor_index;
        u2             attributes_count;
        attribute_info attributes[attributes_count];
    }
    
14. **attributes_count**
 	The value of the property counter represents the number of properties in the property sheet of the current class.
15. **attributes[]**
 	Note that the properties here are not Java Class properties in code(Class field)锛孖t is Java The source file already has some unique properties(Don't talk to fields confusion)锛孉ttribute structure:
 	```xml
 	attribute_info {
 	    u2 attribute_name_index;
 	    u4 attribute_length;
 	    u1 info[attribute_length];
 		}
     ```
     Property list:
     ![Alt](https://myblog-1258060977.cos.ap-beijing.myqcloud.com/cnblog/JVM/%E5%B1%9E%E6%80%A7%E8%A1%A8.png)

###2.2 case study
 First write a paragraph Java Procedures, we are familiar with“ Hello World"
```java
public class Main {
 public static void main(String[] args) {
     System.out.println("Hello World");
 }
}

Using javac Main.java Compilation generation Main.class Document:

cafe babe 0000 0034 001d 0a00 0600 0f09
0010 0011 0800 120a 0013 0014 0700 1507
0016 0100 063c 696e 6974 3e01 0003 2829
5601 0004 436f 6465 0100 0f4c 696e 654e
756d 6265 7254 6162 6c65 0100 046d 6169
6e01 0016 285b 4c6a 6176 612f 6c61 6e67
2f53 7472 696e 673b 2956 0100 0a53 6f75
7263 6546 696c 6501 0009 4d61 696e 2e6a
6176 610c 0007 0008 0700 170c 0018 0019
0100 0b48 656c 6c6f 2057 6f72 6c64 0700
1a0c 001b 001c 0100 044d 6169 6e01 0010
6a61 7661 2f6c 616e 672f 4f62 6a65 6374
0100 106a 6176 612f 6c61 6e67 2f53 7973
7465 6d01 0003 6f75 7401 0015 4c6a 6176
612f 696f 2f50 7269 6e74 5374 7265 616d
3b01 0013 6a61 7661 2f69 6f2f 5072 696e
7453 7472 6561 6d01 0007 7072 696e 746c
6e01 0015 284c 6a61 7661 2f6c 616e 672f
5374 7269 6e67 3b29 5600 2100 0500 0600
0000 0000 0200 0100 0700 0800 0100 0900
0000 1d00 0100 0100 0000 052a b700 01b1
0000 0001 000a 0000 0006 0001 0000 0001
0009 000b 000c 0001 0009 0000 0025 0002
0001 0000 0009 b200 0212 03b6 0004 b100
0000 0100 0a00 0000 0a00 0200 0000 0400
0800 0500 0100 0d00 0000 0200 0e

Start to decipher the above according to the above knowledge Main.class file
First, the first 10 bytes are parsed in order

cafe babe // Magic number, identified as. class bytecode file
0000 0034 //Version 52.0
001d //Constant pool length_ pool_ count 29-1=28

Then start to parse the constant. First, check the first byte: 0a, the corresponding constant type_ Methodref, the corresponding structure is:

CONSTANT_Methodref_info {
    u1 tag;
    u2 class_index;
    u2 name_and_type_index;
}

Tag takes one byte, class_index takes 2 bytes, name_and_type_index takes up two of them. Count them back in turn. Note that 0a is a tag, so the next two bytes are class_index

00 06 // class_index points to the class represented by the sixth constant in the constant pool
00 0f // name_and_type_index points to the method represented by the 15th constant in the constant pool

By analyzing the above methods one by one, we can get the constant pool as follows:

0a // 10 CONSTANT_Methodref
00 06 // Points to the class represented by the sixth constant in the constant pool
00 0f // Points to the method represented by the 15th constant in the constant pool

09 CONSTANT_Fieldref
0010 // Points to the class represented by the 16th constant in the constant pool
0011 // Points to the variable represented by the 17th constant in the constant pool

08 // CONSTANT_String
00 12 // Points to the variable represented by the 18th constant in the constant pool

0a // CONSTANT_Methodref
0013 // Points to the class represented by the 19th constant in the constant pool
0014 // Points to the method represented by the 20th constant in the constant pool

07 // CONSTANT_Class
00 15 // Points to the variable represented by the 21st constant in the constant pool

07 // CONSTANT_Class
0016 // Points to the variable represented by the 22nd constant in the constant pool

01 // CONSTANT_Utf8 identity string
00 // The subscript is 0
06 // 6 bytes
3c 696e 6974 3e //<init>

01 //CONSTANT_Utf8 represents a string
00 // The subscript is 0
03 // 3 bytes
2829 56 // ()v

01 //CONSTANT_Utf8 represents a string
00 // The subscript is 0
04 // 4 bytes
436f 6465 // code

01 //CONSTANT_Utf8 represents a string
00 // The subscript is 0
0f // 15 bytes
4c 696e 654e 756d 6265 7254 6162 6c65 //lineNumberTable

01 //CONSTANT_Utf8 represents a string
00 // The subscript is 0
04 // 4 bytes
6d 6169 6e //main

01 
00
16 
285b 4c6a 6176 612f 6c61 6e67 2f53 7472 696e 673b 2956 //([Ljava/lang/String;)V

0100
0a //10
53 6f75 7263 6546 696c 65 //sourceFile

01 00
09 
4d61 696e 2e6a 6176 61 //Main.java

0c // CONSTANT_NameAndType
0007 //nameIndex:7
0008 //descriptor_index:8

07 //CONSTANT_Class
00 17 // The 21st variable

0c 
0018 
0019

0100
0b
48 656c 6c6f 2057 6f72 6c64 // Hello World

07
00 1a

0c 001b 001c 

0100 
04
4d 6169 6e //main

01 00
10
6a61 7661 2f6c 616e 672f 4f62 6a65 6374 //java/lang/Object

0100 
10
6a 6176 612f 6c61 6e67 2f53 7973 7465 6d // java/lang/System

01 00
03 
6f75 74 // out

01 00
15 
4c6a 6176 612f 696f 2f50 7269 6e74 5374 7265 616d 3b //Ljava/io/PrintStream;

01 00
13 
6a61 7661 2f69 6f2f 5072 696e 7453 7472 6561 6d // java/io/PrintStrea

01 00
07 
7072 696e 746c 6e //println

01 00
15 
284c 6a61 7661 2f6c 616e 672f 5374 7269 6e67 3b29 56 // (ljava/lang/String/String;)V

The structure after the constant pool can continue to be parsed in this way. Now we use the method of java to decompile the. class file, and verify that our above analysis is correct.
Using javap -v Main.class It can be obtained that:

  Last modified 2020-9-29; size 413 bytes
  MD5 checksum 8b2b7cdf6c4121be8e242746b4dea946
  Compiled from "Main.java"
public class Main
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #6.#15         // java/lang/Object."<init>":()V
   #2 = Fieldref           #16.#17        // java/lang/System.out:Ljava/io/PrintStream;
   #3 = String             #18            // Hello World
   #4 = Methodref          #19.#20        // java/io/PrintStream.println:(Ljava/lang/String;)V
   #5 = Class              #21            // Main
   #6 = Class              #22            // java/lang/Object
   #7 = Utf8               <init>
   #8 = Utf8               ()V
   #9 = Utf8               Code
  #10 = Utf8               LineNumberTable
  #11 = Utf8               main
  #12 = Utf8               ([Ljava/lang/String;)V
  #13 = Utf8               SourceFile
  #14 = Utf8               Main.java
  #15 = NameAndType        #7:#8          // "<init>":()V
  #16 = Class              #23            // java/lang/System
  #17 = NameAndType        #24:#25        // out:Ljava/io/PrintStream;
  #18 = Utf8               Hello World
  #19 = Class              #26            // java/io/PrintStream
  #20 = NameAndType        #27:#28        // println:(Ljava/lang/String;)V
  #21 = Utf8               Main
  #22 = Utf8               java/lang/Object
  #23 = Utf8               java/lang/System
  #24 = Utf8               out
  #25 = Utf8               Ljava/io/PrintStream;
  #26 = Utf8               java/io/PrintStream
  #27 = Utf8               println
  #28 = Utf8               (Ljava/lang/String;)V
{
  public Main();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 1: 0

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=1, args_size=1
         0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
         3: ldc           #3                  // String Hello World
         5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
         8: return
      LineNumberTable:
        line 4: 0
        line 5: 8
}
SourceFile: "Main.java"

By comparison, it can be found that the results are consistent with our manual analysis.

Summary

The first part of this article gives a brief introduction to several common problems of JVM. The second part introduces the structure of ClassFile and the specification specified by the JVM for ClassFile (more detailed specifications can be viewed by the official documents). Then, the manual parsing of some bytecode is carried out according to the specification, and the result is compared with that of the JVM. In my opinion, as a partial application layer, programmers do not need to memorize these "specifications", but should jump out of these complicated specifications and master the following points:

  1. With the help of official documents to do a simple reading of bytecode files.
  2. Understanding the role and role of bytecode files in the whole execution process is actually a process of "encoding and decoding". javac generates bytecode files from. java files according to the rules of the JVM, and the JVM parses bytecode files as machine executable instructions according to the specifications.

reference:
https://blog.csdn.net/peng_zhanxuan/article/details/104329859
https://docs.oracle.com/javase/specs/jvms/se11/html/index.html
https://blog.csdn.net/weelyy/article/details/78969412

Posted by apchy at Oct 12, 2020 - 4:13 PM Tag: jvm

4617作文网周易股票预测技巧周易算命取名下载周易诗经里的好名字带沐字的男孩起名苏字的起名苗起名字大全女孩起名取名查看梦想三国之最终之战破解版下载原版周公解梦吧2018周易起名网免费取名潘姓起名女孩八字算命免费四柱八字算命算命先生算的准不周易预测国运周公解梦老人去世周易测名字就名字化妆品起名大全四个字闫宝宝起名字梦到蛇周公解梦原版梦幻之家破解版下载关于铜制品店铺起名周易姓名测试打分 免费测名字打分互联网店铺起名大全2017年周易预测周易起名字免费给女孩起名姓齐香港公司如何起名字有声小说算死命运在线收听八字算命书籍给游戏人物起名淀粉肠小王子日销售额涨超10倍罗斯否认插足凯特王妃婚姻让美丽中国“从细节出发”清明节放假3天调休1天男子给前妻转账 现任妻子起诉要回网友建议重庆地铁不准乘客携带菜筐月嫂回应掌掴婴儿是在赶虫子重庆警方辟谣“男子杀人焚尸”国产伟哥去年销售近13亿新的一天从800个哈欠开始男孩疑遭霸凌 家长讨说法被踢出群高中生被打伤下体休学 邯郸通报男子持台球杆殴打2名女店员被抓19岁小伙救下5人后溺亡 多方发声单亲妈妈陷入热恋 14岁儿子报警两大学生合买彩票中奖一人不认账德国打算提及普京时仅用姓名山西省委原副书记商黎光被逮捕武汉大学樱花即将进入盛花期今日春分张家界的山上“长”满了韩国人?特朗普谈“凯特王妃P图照”王树国3次鞠躬告别西交大师生白宫:哈马斯三号人物被杀代拍被何赛飞拿着魔杖追着打315晚会后胖东来又人满为患了房客欠租失踪 房东直发愁倪萍分享减重40斤方法“重生之我在北大当嫡校长”槽头肉企业被曝光前生意红火手机成瘾是影响睡眠质量重要因素考生莫言也上北大硕士复试名单了妈妈回应孩子在校撞护栏坠楼网友洛杉矶偶遇贾玲呼北高速交通事故已致14人死亡西双版纳热带植物园回应蜉蝣大爆发男孩8年未见母亲被告知被遗忘张立群任西安交通大学校长恒大被罚41.75亿到底怎么缴沈阳一轿车冲入人行道致3死2伤奥运男篮美国塞尔维亚同组周杰伦一审败诉网易国标起草人:淀粉肠是低配版火腿肠外国人感慨凌晨的中国很安全男子被流浪猫绊倒 投喂者赔24万杨倩无缘巴黎奥运男子被猫抓伤后确诊“猫抓病”春分“立蛋”成功率更高?记者:伊万改变了国足氛围奥巴马现身唐宁街 黑色着装引猜测

4617作文网 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化