Detailed Javac the process of compiling a Java file into a class file

Source: Internet
Author: User

The Java compiler is generally divided into the front-end compiler, the JIT (just in time compiler) compiler, the AOT (Ahead of Time compiler) compiler three.

    • Front-end compilers: compilers that compile Java files into class files, currently have the following two main, the Javac and the incremental compilers in Eclipse JDT (ECJ) provided by Sun

    • JIT Compiler: Virtual machine backend run-time compiler, the process of converting bytecode into machine code. C1 provided in the HotSpot VM, C2 compiler

    • AOT compiler: The process of converting a Java file directly to a local machine code. GNU Compiler for the Java (GCJ), Excelsior JET


* After Java 3, in order to enable other languages that are not compiled by Javac to be executed on the Java Virtual machine to enjoy the benefits of compiler optimization So, Java put all the compiler optimization work in the back-end timely compiler.


Sun Javac Compiler

The Sun javac compiler compilation process is divided into three steps, parsing the fill symbol table, the annotation processing of the plug-in annotation processor, the analysis and the bytecode production process

The **javac compiler's entry is the Com.sun.tools.javac.main.JavaCompiler class, and the main logic is concentrated in the compile and Compile2 methods


  1. Parsing the Fill symbol table
    Lexical analysis: Transforming the character stream of a source code into a token collection Com.sun.tools.javac.parser.Scanner class for lexical analysis
    Parsing: Constructing abstract syntax trees (ast,abstract Syntax tree) with tag sequences. The Eclipse AST View plugin can be used to view the abstract syntax tree

  2. Annotation processor
    Java1.5 provides support for annotations (Annotations), which can be understood as a set of plug-ins for abstract syntax trees that can read, modify, and add operations to the abstract syntax tree directly.
    If the syntax tree is modified during the parsing of annotations, the compiler returns to parsing and populating the symbol table until all the inserted annotation processors have not modified the syntax tree.
    The Initprocessannotations () method in the Javaccompiler class initializes the annotation processor, Javacprocessingenvironment class doprocessing () method to generate a new javaccompiler to process the subsequent process of compilation.

  3. Semantic analysis and byte code generation
    After parsing, the compiler gets the abstract syntax tree representation of the program, the syntax tree represents a properly structured source program abstraction, but cannot guarantee that the source program is logical, semantic analysis is to review the structure of the correct source program context.
    Syntax analysis and bytecode generation are divided into four processes: annotation checking, data and control flow analysis, parsing of syntactic sugars, and bytecode generation .
    annotation checking, and data and control flow analysis are primarily analytical checks of the context of the program.
    To solve the syntactic sugar: in Java commonly used to solve the syntax of sugar is generic, in the Java Virtual machine is not supported generics. Grammar is only extended by syntactic sugar.



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.