Tuesday, 8 March 2016

Detailed knowledge of JDK, JRE, JVM and JIT


  • For Beginners
    • JDK (Java Development Kit)
      • JDK contains that part which will be required to develop and run any Java Application.
      • It contains JRE + other required libraries and commands to develop Java applications
      • JDK is platform dependent. it means different JDK for different Operating Systems.
    • JRE (Java Runtime Environment)
      • JRE contains only that part which will be required to run any Java Application. It doesn't contain library which is required to develop Java application.
      • To run Java applications, JRE is required. However, to develop Java applications as well as run them, the JDK is needed.
      • JRE contains JVM + other required libraries to run Java application
      • JRE is platform dependent. it means different JRE for different Operating Systems.
    • JVM (Java Virtual Machine)
      • JVM work over your operating system to provide proper environment for your compiled Java code.
      • JVM only understand bytecode which is generated by javac command. Hence you need to compile your java application(Example.java) so that it can be converted to bytecode format (Example.class). Which then will be used by JVM to run application.
      • JVM only provide environment it needs other library to run application properly.
      • JVM is platform dependent. it means different JVM for different Operating Systems.
    • JIT (Just Intime Compiler)
      • The Just-In-Time (JIT) compiler is a component of the Java Runtime Environment (JRE). It improves the performance of Java applications by compiling bytecodes to native machine code at run time. When a Java program is run JVM launched that interprets the byte code and provides result. At run time, the JVM loads the class files, determines the semantics of each individual bytecode, and performs the appropriate computation. In this process, the JIT compiler helps improve the performance of Java programs by compiling bytecodes into native machine code at run time.

  • For Experience
    • JDK (Java Development Kit)
      • The Java Developer's Kit, or JDK, is a comprehensive set of tools, utilities, documentation, and sample code for developing Java programs. Without it, you wouldn't be able to do much with Java.
      • The Java Developer's Kit contains a variety of tools and Java development information. Following is a list of the main components of the JDK:
        • The Runtime Interpreter
        • The Compiler
        • The Applet Viewer
        • The Debugger
        • The Class File Disassembler
        • The Header and Stub File Generator
        • The Documentation Generator
        • Demos
        • API Source Code
      • When you install JDK, installation folder is often referred as JAVA_HOME. By default, it is not set. All binaries are located inside JAVA_HOME/bin which includes javac, java and other binaries and they must be in your system PATH in order to compile and execute Java programs.
      • We have two JDK which we have heard most of the times i.e. JDK (also known as Oracle JDK) and OpenJDK
        • JDK was previously called SUN JDK but after Sun takeover by Oracle, it was named as Oracle JDK and maintained by Oracle corporation
        • OpenJDK is an open source implementation of the Java SE platform with contribution from Oracle and open Java community and now it is the official reference implementation for Java Standard Edition from Java SE 7.
        • OpenJDK is released under license GPL v2 wherein Oracle JDK is licensed under Oracle Binary Code License Agreement.
        • There is no major technical difference between Oracle JDK and OpenJDK
          • Oracle JDK includes, Oracle’s implementation of Java Plugin and Java WebStart and some third party closed source and open source components like Rhino and Nashorn ( both are Java Script Engines).
        • JRockit was the Oracle’s JVM and from Java SE 7, HotSpot and JRockit merged into a single JVM. So now we have only the merged HotSpot JVM available.
      • you can download jdk from oracle website: http://www.oracle.com/technetwork/java/javase/downloads/index.html
    • JRE (Java Runtime Environment)
      • The Java Runtime Environment (JRE) provides the libraries, the Java Virtual Machine, and other components to run applets and applications written in the Java programming language. In addition, two key deployment technologies are part of the JRE: Java Plug-in, which enables applets to run in popular browsers; and Java Web Start, which deploys standalone applications over a network.The JRE does not contain tools and utilities such as compilers or debuggers for developing applets and applications.
      • As above said, Java Runtime Environment contains JVM, class libraries, and other supporting files. Actually JVM runs the program, and it uses the class libraries, and other supporting files provided in JRE. If you want to run any java program, you need to have JRE installed in the system.
      • Java is every where in browser, in mobile, in TV or in set-top boxes and if you are into Java programming language than you know that Java code which is bundled in JAR (Java archive) file require Java virtual machine JVM to execute it. Now JVM is an executable or program like any other program and you can install that into your machine. You have seen browser often suggesting download JRE to run a Java Applet downloaded from Internet. Various version of JRE are available in java.oracle.com and most of the user who just want to execute Java program inside browser or standalone downloads JRE. All browsers including Internet Explorer, Firefox and Chrome can work with JRE.
    • JVM (Java Virtual Machine)
      • The Java Virtual Machine provides a platform-independent way of executing code; That mean compile once in any machine and run it any where (any machine).
      • The Java Virtual machine (JVM) is the virtual machine that run the Java bytecodes. The JVM doesn't understand Java source code, that's why you compile your *.java files to obtain *.classfiles that contain the bytecodes understandable by the JVM. It's also the entity that allows Java to be a "portable language" (write once, run anywhere). Indeed there are specific implementations of the JVM for different systems, the aim is that with the same bytecodes they all give the same results.
      • As we all aware when we compile a Java file, output is not an ‘exe’ but it’s a ‘.class’ file. ‘.class’ file consists of Java byte codes which are understandable by JVM. Java Virtual Machine interprets the byte code into the machine code depending upon the underlying operating system and hardware combination. It is responsible for all the things like garbage collection, array bounds checking, etc.
      • There are different JVM implementations are there. These may differ in things like performance, reliability, speed, etc. These implementations will differ in those areas where Java specification doesn’t mention how to implement the features, like how the garbage collection process works is JVM dependent, Java spec doesn’t define any specific way to do this.
      • Java Virtual machine is at heart of Java programming language and provide several feature to Java programmer including Memory Management and Garbage Collection, Security and other system level services. Java Virtual Machine can be customized e.g we can specify starting memory or maximum memory of heap size located inside JVM. In short Java Virtual Machine or JVM is the one who provides Platform independence to Java.
      • For more details of JVM, you can read these two URLs:
    • JIT (Just Intime Compiler)
      • The Just-In-Time (JIT) compiler is a component of the Java Runtime Environment (JRE). It improves the performance of Java applications by compiling bytecodes to native machine code at run time. When a Java program is run JVM launched that interprets the byte code and provides result. At run time, the JVM loads the class files, determines the semantics of each individual bytecode, and performs the appropriate computation. In this process, the JIT compiler helps improve the performance of Java programs by compiling bytecodes into native machine code at run time.
      • The JIT compiler is enabled by default, and is activated when a Java method is called. The JIT compiler compiles the byte codes of that method into native machine code, compiling it "just in time" to run. When a method has been compiled, the JVM calls the compiled code of that method directly instead of interpreting it. Theoretically, if compilation did not require processor time and memory usage, compiling every method could allow the speed of the Java program to approach that of a native application.
      • JIT compilation does require processor time and memory usage. When the JVM first starts up, thousands of methods are called. Compiling all of these methods can significantly affect start-up time, even if the program eventually achieves very good peak performance.The JIT compiler can be disabled, in which case the entire Java program will be interpreted. Disabling the JIT compiler is not recommended except to diagnose or work around JIT compilation problems.
  • Below are some Images which I have created to give you more detailed knowledge about JVM and JIT compiler
JVM Architecture