In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to learn Java files to .Class files in JVM true fragrance series". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to learn Java files to .Class files in the JVM true fragrance series".
What is JVM?
The full name of JVM is Java Virtual Machine, which is the Java virtual machine we are familiar with. It can recognize files with the .class suffix, parse its instructions, and finally call functions on the operating system to complete the operations we want.
There may be some small partners who have learned the programs developed by Candles.Compiled into binary files, they can be directly executed, and can be recognized by the operating system.
But the Java program we open is different. After using the .class file compiled by the javac command, the operating system cannot recognize it. It needs to do a conversion corresponding to JVM before the operating system can recognize it.
Why can't we run compiled binaries directly on the operating system, like C++? Instead of building a virtual machine in the middle layer between the program and the operating system?
This is the beauty of JVM. As we all know, Java is a highly abstract language that provides a range of features such as automatic memory management. It is not possible to implement these features directly on the operating system, so JVM is required to do a series of transformations.
When we first started to learn Java, we knew that there was a Write Once Run Everywhere. That is, we have written a java file and compiled it into a .class file, which can be run on various systems.
In fact, there is a premise here, we need to install the corresponding JVM in the corresponding operating system, and then our .class file can run.
For example: Windows operating system has the corresponding JDK installation version, Linux also has the corresponding JDK installation version and so on.
Get to know JDK
Java Development Kit (JDK) is a software development kit for Java developers of Sun (acquired by Oracle). Since the launch of Java, JDK has become the most widely used Java SDK (Software development kit).
According to an unofficial survey, JDK8 is currently the most widely used version.
JDK14 will receive security updates in April and July and will be replaced by a non-LTS version of JDK 15, which expires in September. JDK14 includes 16 new features, such as JDK Flight Recorder event flow, pattern matching and switch expressions.
Since JDK9, Oracle has adopted a new release cycle: a version every six months and a version of LTS every three years. JDK14 is the fourth version released after JDK9, which is a non-LTS version, and the latest LTS version is JDK11.
Here is the JDK version
Just look familiar and keep abreast of JDK version updates and new features.
Official website address: https://www.oracle.com/java/
The installation of JDK will be omitted here.
The relationship among JDK, JRE and JVM
The related concepts of JDK and JVM have been mentioned above.
JRE full-process Java Runtime Environment is an indispensable running environment for running programs written based on Java language. It is also through it that Java developers are able to release their own programs to users for use.
What is the relationship among the three?
For the relationship between the three, please see the official website.
Https://docs.oracle.com/javase/8/docs/index.html
JDK includes JRE as well as JDK, while JRE also includes JDK. Scope relationship: JDK > JRE > JVM
".java" file to ".class" file
`javac` command
Write a HelloWorld.java file
The content is an introduction to Java.
Public class HelloWorld {public static void main (String [] args) {System.out.println ("Hello world");}}
Open CMD, enter the current directory, and use the command
Javac HelloWorld.java
Compile the HelloWorld.class.
Compilation process
What exactly does this javac command process do?
These operations are roughly done behind javac.
This process
1. Lexical analysis
Read the source code, byte by byte read, find out which we defined keywords (such as Java if, else, for, while and other keywords, identify which if is legal keywords, which are not), this is the lexical analyzer for lexical analysis process, the result is to find out the standardized Token stream from the source code.
2. Grammatical analysis
The parser parses the Token stream after lexical analysis, and this step checks whether these keyword combinations conform to the Java language specification again (such as whether a Boolean judgment expression is followed by if). The result of lexical analysis is to form an abstract syntax tree that conforms to the Java language specification.
3. Semantic analysis
Semantic analysis is carried out through the semantic analyzer. Speech analysis mainly converts some difficult and complex grammars into simpler grammars, resulting in the simplest grammar (such as converting foreach into for loops, annotations, etc.), and finally forms an annotated abstract syntax tree, which is closer to the grammar rules of the target language.
4. Generate bytecode
Bytecode is generated by bytecode generator and bytecode is generated by an abstract tree according to the annotated syntax, that is, one data structure is transformed into another. Finally, the .class file we want is generated.
View the contents of the class file using hexadecimal
I only use Notepad++, to select the text → plug-in → Converter → ASCII- > HEX
The class file begins with
CAFEBABE
To learn the meaning of the hexadecimal bytecode here, please refer to
Https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html
Javap views the contents of the class file
Javap is a Java class file resolver that can be decompiled (that is, decompiled files compiled by javac) or can view bytecode generated by the java compiler.
Create a new User.java source file, compile with javac, and generate User.classs.
Package com.tian.demo.test; public class User {private int age = 22; private String name = "tian"; public int addAge () {return age = age + 1;} public static void main (String [] args) {}}
Use the javap command
Javap-v User.class > log.txt
Open log.txt
Classfile / D:/workspace/new/demo/src/main/java/com/tian/demo/test/User.class Last modified 2020-11-5 Size 441 bytes MD5 checksum 2fa72d3f53bd9f138e0bfae82aba67e3 Compiled from "User.java" public class com.tian.demo.test.User minor version: 0 major version: 52 flags: ACC_PUBLIC ACC_SUPER Constant pool: # 1 = Methodref # 6.room21 / / java/lang/Object. "": () V # 2 = Fieldref # 5.room22 / / com/tian/demo/test/User.age:I # 3 = String # 23 / / tian # 4 = Fieldref # 5.room24 / / Com/tian/demo/test/User.name:Ljava/lang/String # 5 = Class # 25 / / com/tian/demo/test/User # 6 = Class # 26 / / java/lang/Object # 7 = Utf8 age # 8 = Utf8 I # 9 = Utf8 name # 10 = Utf8 Ljava/lang/String # 11 = Utf8 # 12 = Utf8 () V # 13 = Utf8 Code # 14 = Utf8 LineNumberTable # 15 = Utf8 addAge # 16 = Utf8 () I # 17 = Utf8 main # 18 = Utf8 ([Ljava/lang/String ) V # 19 = Utf8 SourceFile # 20 = Utf8 User.java # 21 = NameAndType # 11 / 12 / "": () V # 22 = NameAndType # 7 / age:I # 23 = Utf8 tian # 24 = NameAndType # 9 / name:Ljava/lang/String # 25 = Utf8 com/tian/demo/test/User # 26 = Utf8 java/lang/Object {public com.tian.demo.test.User () Descriptor: () V flags: ACC_PUBLIC Code: stack=2, locals=1 Args_size=1 0: aload_0 1: invokespecial # 1 / / Method java/lang/Object. ": () V 4: aload_0 5: bipush 22 7: putfield # 2 / / Field age:I 10: aload_0 11: ldc # 3 / / String tian 13: putfield # 4 / / Field name:Ljava/lang/String 16: return LineNumberTable: line 3: 0 line 4: 4 line 5: 10 public int addAge () Descriptor: () I flags: ACC_PUBLIC Code: stack=3, locals=1 Args_size=1 0: aload_0 1: aload_0 2: getfield # 2 / / Field age:I 5: iconst_1 6: iadd 7: dup_x1 8: putfield # 2 / / Field age:I 11: ireturn LineNumberTable: line 8: 0 Public static void main (java.lang.String []) Descriptor: (Ljava/lang/String;) V flags: ACC_PUBLIC, ACC_STATIC Code: stack=0, locals=1, args_size=1 0: return LineNumberTable: line 13: 0} SourceFile: "User.java"
Magic number and class file version
Constant pool
Access flag
Class index, parent index, interface index
Field table collection
Method table collection
Property sheet collection
Then JVM can read the User.class file for parsing and a series of operations.
The above is our Java file to class file.
IT Technology sharing Community
Personal blog site: https://programmerblog.xyz knows JVM
What is JVM?
The full name of JVM is Java Virtual Machine, which is the Java virtual machine we are familiar with. It can recognize files with the .class suffix, parse its instructions, and finally call functions on the operating system to complete the operations we want.
There may be some small partners who have learned the programs developed by Candles.Compiled into binary files, they can be directly executed, and can be recognized by the operating system.
But the Java program we open is different. After using the .class file compiled by the javac command, the operating system cannot recognize it. It needs to do a conversion corresponding to JVM before the operating system can recognize it.
Why can't we run compiled binaries directly on the operating system, like C++? Instead of building a virtual machine in the middle layer between the program and the operating system?
This is the beauty of JVM. As we all know, Java is a highly abstract language that provides a range of features such as automatic memory management. It is not possible to implement these features directly on the operating system, so JVM is required to do a series of transformations.
When we first started to learn Java, we knew that there was a Write Once Run Everywhere. That is, we have written a java file and compiled it into a .class file, which can be run on various systems.
In fact, there is a premise here, we need to install the corresponding JVM in the corresponding operating system, and then our .class file can run.
For example: Windows operating system has the corresponding JDK installation version, Linux also has the corresponding JDK installation version and so on.
Get to know JDK
Java Development Kit (JDK) is a software development kit for Java developers of Sun (acquired by Oracle). Since the launch of Java, JDK has become the most widely used Java SDK (Software development kit).
According to an unofficial survey, JDK8 is currently the most widely used version.
JDK14 will receive security updates in April and July and will be replaced by a non-LTS version of JDK 15, which expires in September. JDK14 includes 16 new features, such as JDK Flight Recorder event flow, pattern matching and switch expressions.
Since JDK9, Oracle has adopted a new release cycle: a version every six months and a version of LTS every three years. JDK14 is the fourth version released after JDK9, which is a non-LTS version, and the latest LTS version is JDK11.
Here is the JDK version
Just look familiar and keep abreast of JDK version updates and new features.
Official website address: https://www.oracle.com/java/
The installation of JDK will be omitted here.
The relationship among JDK, JRE and JVM
The related concepts of JDK and JVM have been mentioned above.
JRE full-process Java Runtime Environment is an indispensable running environment for running programs written based on Java language. It is also through it that Java developers are able to release their own programs to users for use.
What is the relationship among the three?
For the relationship between the three, please see the official website.
Https://docs.oracle.com/javase/8/docs/index.html
JDK includes JRE as well as JDK, while JRE also includes JDK. Scope relationship: JDK > JRE > JVM
".java" file to ".class" file
`javac` command
Write a HelloWorld.java file
The content is an introduction to Java.
Public class HelloWorld {public static void main (String [] args) {System.out.println ("Hello world");}}
Open CMD, enter the current directory, and use the command
Javac HelloWorld.java
Compile the HelloWorld.class.
Compilation process
What exactly does this javac command process do?
These operations are roughly done behind javac.
This process
1. Lexical analysis
Read the source code, byte by byte read, find out which we defined keywords (such as Java if, else, for, while and other keywords, identify which if is legal keywords, which are not), this is the lexical analyzer for lexical analysis process, the result is to find out the standardized Token stream from the source code.
2. Grammatical analysis
The parser parses the Token stream after lexical analysis, and this step checks whether these keyword combinations conform to the Java language specification again (such as whether a Boolean judgment expression is followed by if). The result of lexical analysis is to form an abstract syntax tree that conforms to the Java language specification.
3. Semantic analysis
Semantic analysis is carried out through the semantic analyzer. Speech analysis mainly converts some difficult and complex grammars into simpler grammars, resulting in the simplest grammar (such as converting foreach into for loops, annotations, etc.), and finally forms an annotated abstract syntax tree, which is closer to the grammar rules of the target language.
4. Generate bytecode
Bytecode is generated by bytecode generator and bytecode is generated by an abstract tree according to the annotated syntax, that is, one data structure is transformed into another. Finally, the .class file we want is generated.
View the contents of the class file using hexadecimal
I only use Notepad++, to select the text → plug-in → Converter → ASCII- > HEX
The class file begins with
CAFEBABE
To learn the meaning of the hexadecimal bytecode here, please refer to
Https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html
Javap views the contents of the class file
Javap is a Java class file resolver that can be decompiled (that is, decompiled files compiled by javac) or can view bytecode generated by the java compiler.
Create a new User.java source file, compile with javac, and generate User.classs.
Package com.tian.demo.test; public class User {private int age = 22; private String name = "tian"; public int addAge () {return age = age + 1;} public static void main (String [] args) {}}
Use the javap command
Javap-v User.class > log.txt
Open log.txt
Classfile / D:/workspace/new/demo/src/main/java/com/tian/demo/test/User.class Last modified 2020-11-5 Size 441 bytes MD5 checksum 2fa72d3f53bd9f138e0bfae82aba67e3 Compiled from "User.java" public class com.tian.demo.test.User minor version: 0 major version: 52 flags: ACC_PUBLIC ACC_SUPER Constant pool: # 1 = Methodref # 6.room21 / / java/lang/Object. "": () V # 2 = Fieldref # 5.room22 / / com/tian/demo/test/User.age:I # 3 = String # 23 / / tian # 4 = Fieldref # 5.room24 / / Com/tian/demo/test/User.name:Ljava/lang/String # 5 = Class # 25 / / com/tian/demo/test/User # 6 = Class # 26 / / java/lang/Object # 7 = Utf8 age # 8 = Utf8 I # 9 = Utf8 name # 10 = Utf8 Ljava/lang/String # 11 = Utf8 # 12 = Utf8 () V # 13 = Utf8 Code # 14 = Utf8 LineNumberTable # 15 = Utf8 addAge # 16 = Utf8 () I # 17 = Utf8 main # 18 = Utf8 ([Ljava/lang/String ) V # 19 = Utf8 SourceFile # 20 = Utf8 User.java # 21 = NameAndType # 11 / 12 / "": () V # 22 = NameAndType # 7 / age:I # 23 = Utf8 tian # 24 = NameAndType # 9 / name:Ljava/lang/String # 25 = Utf8 com/tian/demo/test/User # 26 = Utf8 java/lang/Object {public com.tian.demo.test.User () Descriptor: () V flags: ACC_PUBLIC Code: stack=2, locals=1 Args_size=1 0: aload_0 1: invokespecial # 1 / / Method java/lang/Object. ": () V 4: aload_0 5: bipush 22 7: putfield # 2 / / Field age:I 10: aload_0 11: ldc # 3 / / String tian 13: putfield # 4 / / Field name:Ljava/lang/String 16: return LineNumberTable: line 3: 0 line 4: 4 line 5: 10 public int addAge () Descriptor: () I flags: ACC_PUBLIC Code: stack=3, locals=1 Args_size=1 0: aload_0 1: aload_0 2: getfield # 2 / / Field age:I 5: iconst_1 6: iadd 7: dup_x1 8: putfield # 2 / / Field age:I 11: ireturn LineNumberTable: line 8: 0 Public static void main (java.lang.String []) Descriptor: (Ljava/lang/String;) V flags: ACC_PUBLIC, ACC_STATIC Code: stack=0, locals=1, args_size=1 0: return LineNumberTable: line 13: 0} SourceFile: "User.java"
Magic number and class file version
Constant pool
Access flag
Class index, parent index, interface index
Field table collection
Method table collection
Property sheet collection
Then JVM can read the User.class file for parsing and a series of operations.
The above is our Java file to class file.
At this point, I believe you have a deeper understanding of "how to learn Java files to .Class files in JVM series". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 248
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.