Procedures for compiling and running Java programs

Source: Internet
Author: User

The whole process of compiling and running Java is quite tedious, and this article simply explains the whole process through a simple program.

For example, a Java program is created from a source file to a program that takes two major steps: 1, the source file is compiled by the compiler into bytecode (bytecode) 2, and the bytecode is run by the Java Virtual machine interpretation. Because Java programs are compiled and run by the JVM's interpretation, Java is called a semi-explanatory language ("semi-interpreted" language).


Figure 1 Java program compilation run process

The following is a Java program that illustrates the entire process of Java programs from compilation to Last run. The code is as follows:

Java code  
  1. Mainapp.java
  2. Public class Mainapp {
  3. public static void Main (string[] args) {
  4. Animal Animal = new Animal ("Puppy");
  5. Animal.printname ();
  6. }
  7. }
  8. Animal.java
  9. Public class Animal {
  10. Public String name;
  11. Public Animal (String name) {
  12. this.name = name;
  13. }
  14. public void Printname () {
  15. System.out.println ("Animal [" +name+"]");
  16. }
  17. }

First step (compile): After the source file is created, the program is first compiled into a. class file. When Java compiles a class, if the class depends on the classes that have not yet been compiled, the compiler will compile the dependent class and then reference it, or else direct reference, which is a bit like make. If the Java compiler cannot find the. class file or the. Java source file of the class to which the class is dependent under the specified directory, the compiler is called "Cant find Symbol" error.

The compiled bytecode file format is mainly divided into two parts: Constant PoolAnd Method byte code。 A constant pool records all tokens (class names, member variable names, and so on) that have occurred in the code, as well as symbolic references (method references, member variable references, and so on); The method byte code is the bytecode of each method in the class. Here's mainapp.class. The structure of the. class file can be clearly seen through disassembly: Figure 2 Mainapp class constant pool
Figure 3 Mainapp class method byte codeSecond Step (run): Java class running process can be divided into two procedures: 1, class loading 2, the execution of the class. It is important to note that the JVM will not load the class until the first time the program has actively used the class. That is, the JVM does not load a program into memory in the first place, but loads it only once when it has to be used. Here are the detailed steps to run the program:
    1. After compiling the Java program to get the Mainapp.class file, tap Java appmain on the command line. The system starts a JVM process, and the JVM process finds a binary file named Appmain.class from the classpath path and loads the Mainapp class information into the method area of the runtime data area, a process called Mainapp class loading.
    2. The JVM then finds the main function entry for Appmain and begins executing the main function.
    3. The first command of the main function is animal animal = new Animal ("Puppy"), which is to have the JVM create a Animal object, but this time there is no information about the animal class in the method area, so the JVM immediately loads the animal class, Put the type information of the animal class into the method area.
    4. After loading the animal class, the first thing the Java Virtual machine does is allocate memory for a new animal instance in the heap area, and then call the constructor to initialize the animal instance. This animal instance holds a reference to the type information for the animal class of the method area, which contains the method table, the underlying implementation of the Java dynamic binding.
    5. When using Animal.printname (), the JVM finds the animal object based on the animal reference and then navigates to the method table for the type information of the animal class in the method area based on the reference held by the animal object, obtaining Printname () The address of the byte code of the function.
    6. Start running the Printname () function.

Figure 4 Java program running process Special Description: All public and protected instance methods in Java class adopt dynamic binding mechanism, all private methods, static methods, constructors and initialization method <clinit> are all using static binding mechanism. The method table is used when the dynamic binding mechanism is used, and the static bindings are not used. This article just tells the Java program to run the approximate process, so it is not a fine distinction. The process described in this article is very coarse, and readers who want to know more about it are asked to consult other materials. Where there is falsehood, please correct me.

Procedures for compiling and running Java programs

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.