Monday, 5 December 2016

Complete flow of JVM compilation and run program.

Complete flow of JVM compilation and run program.

Complete flow of JVM.


1.   JVM loaded in the memory.
2.   Class loader invoked by the JVM.
3.   Test.class file loaded by the class loader.
4.   .class file provided to the byte code verifier.
5.   After the successful verification class loaded into the class area.
6.   main() method started.
7.   Frame of the main() method created.
8.   Execution of the instruction of the main() method.
      9. Execution of main() method completed and frame gets destroyed.

Steps to make the .jar file

Steps to make the .jar file

Step 1: - prepare the .class file by the javac these are the .class file that we want to move in the .jar file.
               C:\java>javac abc.java
.class will be generated.

Step 2: -Execute the jar command
command to make jar file
Step 3: - After creating the sample.jar file just place it in the jre\lib\ext folder.
Step 4: - Delete the .class file from the current directory
Step 5: - Run the program java Test
                                                That contains main method
Step 6: - System class loader becomes fail to load Test.class but extended class loader will load successfully from 
            C:\Program Files\java\jdk1.8.0\jre\bin\ext\sample.jar

THE ARCHITECTURE OF THE JVM

THE ARCHITECTURE OF THE JVM.

Architecture of JVM


1.      Class loader.
2.      Byte code verifier.
3.      Memory area.
i.              Class area.
ii.            Heap area.
iii.          Stack area.
iv.          Native area.
v.            PC register.
    4. Execution engine.

1.      Class loader
               Class loader is the utility in the JVM which is responsible to load the .class file from hard disk to JVM.

There are Three Types of class Loader
1.1      Boot Strap class loader
1.2      System class loader
1.3      Extended class loader

1.1     Boot Strap class loader
·         Boot strap class loader loads the .class file from the java library available in the JRE.
   C:\Program Files\java\jdk1.8.0\jre\lib\rt.jar
a.       .jar file is the compressed file.
b.      rt.jar file contains the approximately 5000 classes within the different packages.

[Note: - If were using the class of the java library in our class then all that classes also gets loaded in the JVM.]

For Ex: - In the last program System class, Sting class and Integer class are the library classes that we were using in our Test class.
·         Along with our test class there are three library classes also gates loaded into the JVM.

1.2     System class loader
·         System class loader is responsible to load the user define classes from the class path.
·         The current directory that we are opened in the DOS is the default class path.

1.3     Extended class loader
               This class loader will load the .class file from the jre\lib\ext folder.
         C:\Program Files\Java\jdk1.8.0\jre\lib\ext

               In the ext folder the .class file should not be placed directly rather must be placed within the .jar file.
         Jar tool in java is use to make a .jar file and also use to uncompress the .jar file.
               .jar tool is also available in the
         C:\Program Files\Java\jdk1.8.0\jre\bin


Steps to make the .jar file

Step 1: - prepare the .class file by the javac these are the .class file that we want to move in the .jar file.
               C:\java>javac abc.java
.class will be generated.

Step 2: -Execute the jar command
command to make jar file
Step 3: - After creating the sample.jar file just place it in the jre\lib\ext folder.
Step 4: - Delete the .class file from the current directory
Step 5: - Run the program java Test
                                                That contains main method
Step 6: - System class loader becomes fail to load Test.class but extended class loader will load successfully from
            C:\Program Files\java\jdk1.8.0\jre\bin\ext\sample.jar


Sequence of the class loader to load any class
1.      Extended class loader.
2.      System class loader.
3.      Boot strap class loader 



2.     Byte code verifier
·         Byte code verifier is the tool in the JVM that verify to the code of the .class file loader by the class loader
·         In the content in the .class file is found correct by the byte code verifier than the content of the .class file copied into the memory area of the JVM. 

3.     Memory area
               Memory area in JVM there are five types of memory areas in the JVM
3.1      Class area
3.2      Heap area
3.3      Stack area
3.4      Native area
3.5.    PC register

3.1   Class area
·         Class areas in the JVM are used to hold the .class file which is loaded by the class loader.
·         They are only one class area in the JVM which is shared by all the threads.
 
Class area

3.2     Heap Area
               All the objects always are created in the heap area.
               There is only one heap area as per JVM which is shared by all the thread running in the                        JVM.
            
Heap area
3.3     Stack Area
               Stack area is used in the method execution.
               That means when any method is invoked then immediately a new frame is created in the                      stack and as the method completed then the frame of that method gets destroyed.
Stack area

 [Note: - There is the separate stack in the JVM for the separate threads.]

3.4     Native Area
·         Native area in the JVM is use to contain the native code.
·         A native code is the code written in java in the special manner by using the JNI (Java native Interface).
·         Through the native code is the function of the C & C++ can be invoked.
·         In the operating system the internal library is developed in the C language that is invoked by JVM through the native code.
·         There is a separate native code for the separate thread.

3.5     PC Register
·         A PC register contains the next instruction to be executed.
·         There are shape rate PC register for each thread.

4. Execution engine.
The bytecode that is assigned to the runtime data areas in the JVM via class loader is executed by the execution engine. The execution engine reads the Java Bytecode in the unit of instruction. It is like a CPU executing the machine command one by one.

Working of JIT (Just In Time) compiler.

 Working of JIT (Just In Time) compiler.

  • JIT instance for just in time compiler which is responsible to convert the byte code into the executable code in the following manner.
  • When a method calling is encountered first time, the JIT converts there instructions and provided then to the operating system for the execution and at that same time also stored the converted instruction in the temporary cache.
  • When the method calling this encountered again then the JIT fix up the converted instructions from the temporary cache and provided to the operating system.
JIT working

1.      Java command issued to run the Test class and immediately JVM gets loaded into the RAM.
2.      Test class loaded into JVM that means byte code of the total class loaded.
3.      JIT started to convert the instruction of main method.
4.1      M1 ( ) method calling encountered.
4.2  Instruction of M1 ( ) method converted by the JIT and stored into the cache.
4.3  Instruction provide to the operating system for the execution.
5.1  Calling of M1 ( ) method again.
      5.2 JIT pickup the instruction from the cache and provide to the operating system.

Difference between Compiler and Interpreter

Difference between Compiler and Interpreter



  • Initially JVM in java was the interpreter but now the JVM is the compiler come interpreter both.
  • As the conversion tool JVM has JIT (Just In Time Compiler).

Except the value from command line and find out the minimum and maximum values.

Except the value from command line and find out the minimum and maximum values.



Except the value from command line and check how many values are even or odd?

Except the value from command line and check how many values are even or odd