JDK, JVM and JRE
JDK, JVM and JRE
JDK, JVM and JRE
It is:
A specification where working of Java Virtual Machine is specified. But
implementation provider is independent to choose the algorithm. Its
implementation has been provided by Sun and other companies.
An implementation is a computer program that meets the requirements of the
JVM specification
Runtime Instance Whenever you write java command on the command prompt to
run the java class, an instance of JVM is created.
Difference betweem JDK, JRE and JVM
To understand the difference between these three, let us consider the following
diagram.
JDK – Java Development Kit (in short JDK) is Kit which provides the environment
to develop and execute(run) the Java program. JDK is a kit(or package) which
includes two things
1. Development Tools(to provide an environment to develop your java
programs)
2. JRE (to execute your java program).
Note : JDK is only used by Java Developers.
JRE – Java Runtime Environment (to say JRE) is an installation package which
provides environment to only run(not develop)the java program(or
application)onto your machine. JRE is only used by them who only wants to run
the Java Programs i.e. end users of your system.
JVM – Java Virtual machine(JVM) is a very important part of both JDK and JRE
because it is contained or inbuilt in both. Whatever Java program you run using
JRE or JDK goes into JVM and JVM is responsible for executing the java
program line by line hence it is also known as interpreter.
How does JRE and JDK works?
What does JRE consists of?
JRE consists of the following components:
Deployment technologies, including deployment, Java Web Start and Java Plug-
in.
User interface toolkits, including Abstract Window Toolkit (AWT), Swing, Java
2D, Accessibility, Image I/O, Print Service, Sound, drag and drop (DnD) and input
methods.
Integration libraries, including Interface Definition Language (IDL), Java
Database Connectivity (JDBC), Java Naming and Directory Interface (JNDI),
Remote Method Invocation (RMI), Remote Method Invocation Over Internet Inter-
Orb Protocol (RMI-IIOP) and scripting.
Other base libraries, including international support, input/output (I/O), extension
mechanism, Beans, Java Management Extensions (JMX), Java Native Interface
(JNI), Math, Networking, Override Mechanism, Security, Serialization and Java for
XML Processing (XML JAXP).
Lang and util base libraries, including lang and util, management, versioning, zip,
instrument, reflection, Collections, Concurrency Utilities, Java Archive (JAR),
Logging, Preferences API, Ref Objects and Regular Expressions.
Java Virtual Machine (JVM), including Java HotSpot Client and Server Virtual
Machines.
How does JRE works?
To understand how the JRE works let us consider a Java source file saved
as Example.java. The file is compiled into a set of Byte Code that is stored in a “.class”
file. Here it will be “Example.class“.
The following diagram depicts what is done at compile time.
Intrepreter
At runtime the Byte Code is loaded, checked and run by the interpreter. The interpreter
has the following two functions:
To understand the interactions between JDK and JRE consider the following diagram.
JVM Architecture?
JVM(Java Virtual Machine) acts as a run-time engine to run Java applications. JVM is
the one that actually calls the main method present in a java code. JVM is a part of
JRE(Java Runtime Environment).
Java applications are called WORA (Write Once Run Anywhere). This means a
programmer can develop Java code on one system and can expect it to run on any
other Java enabled system without any adjustment. This is all possible because of JVM.
When we compile a .java file, .class files(contains byte-code) with the same class
names present in .java file are generated by the Java compiler. This .class file goes into
various steps when we run it. These steps together describe the whole JVM.
Note : JVM follow Delegation-Hierarchy principle to load classes. System class loader
delegate load request to extension class loader and extension class loader delegate
request to boot-strap class loader. If class found in boot-strap path, class is loaded
otherwise request again transfers to extension class loader and then to system class
loader. At last if system class loader fails to load class, then we get run-time
exception java.lang.ClassNotFoundException.
JVM Memory
Method area :In method area, all class level information like class name, immediate
parent class name, methods and variables information etc. are stored, including static
variables. There is only one method area per JVM, and it is a shared resource.
Heap area :Information of all objects is stored in heap area. There is also one Heap
Area per JVM. It is also a shared resource.
Stack area :For every thread, JVM create one run-time stack which is stored here.
Every block of this stack is called activation record/stack frame which store methods
calls. All local variables of that method are stored in their corresponding frame. After a
thread terminate, it’s run-time stack will be destroyed by JVM. It is not a shared
resource.
PC Registers :Store address of current execution instruction of a thread. Obviously
each thread has separate PC Registers.
Native method stacks :For every thread, separate native stack is created. It stores
native method information.
Execution Engine
Execution engine execute the .class (bytecode). It reads the byte-code line by line, use
data and information present in various memory area and execute instructions. It can be
classified in three parts :-
Interpreter : It interprets the bytecode line by line and then executes. The
disadvantage here is that when one method is called multiple times, every time
interpretation is required.
Just-In-Time Compiler(JIT) : It is used to increase efficiency of interpreter.It
compiles the entire bytecode and changes it to native code so whenever
interpreter see repeated method calls,JIT provide direct native code for that part so
re-interpretation is not required,thus efficiency is improved.
Garbage Collector : It destroy un-referenced objects.For more on Garbage
Collector,refer Garbage Collector.
Java Native Interface (JNI) :
It is a interface which interacts with the Native Method Libraries and provides the native
libraries(C, C++) required for the execution. It enables JVM to call C/C++ libraries and
to be called by C/C++ libraries which may be specific to hardware.
Native Method Libraries :
It is a collection of the Native Libraries(C, C++) which are required by the Execution
Engine.