This Tutorial gives a Detailed Description of Java as a Platform and its Components i.e. JRE, JDK, Java Virtual Machine and Difference Between JRE vs JDK:
Java has been described as a programming language as well as the platform. In our introductory tutorial, we already discussed that a platform is a software environment using which we can execute programs or applications.
In this tutorial, we will discuss Java, as a platform. We will also discuss Java platform components and see how they work.
=> Read Through The Easy Java Training Series.
What You Will Learn:
Java platform is a software or collection of programs that help us to execute applications written in Java programming language. A Java platform consists of a Java compiler, a set of libraries, and an execution engine.
Java platform is independent of any particular OS which makes Java programming language a platform-independent language.
Java platform consists of the following components.
- Java language
- The Java Development Kit (JDK)
- The Java Runtime Environment (JRE)
- The Java Compiler
- The Java Virtual Machine (JVM)
Apart from the above main components, the Java platform also contains garbage collectors, a set of libraries and other additional components and tools that are required to efficiently run the Java applications.
Let’s consider the following diagram that depicts the relationship between all these components.
So from the above diagram, we can conclude that,
JRE = JVM + library classes.
JDK = JRE + Developer tools.
Before we take up each of the components, let’s understand the execution flow of Java program execution first.
The following diagram shows the flow of a Java program.
So the above diagram shows a simple execution flow for a Java program. A program written in Java language is compiled by the Java compiler (javac). Unlike other language compilers that generate machine code after compilation, the Javac compiler generates a class file consisting of byte code.
Now, this class file is given to Java Virtual Machine (JVM) that translates the class file into an object file containing machine-dependent code. Then this code is executed by the machine.
Let’s discuss each of these components in detail.
Java is a programming language that the Java platform uses. Java is an object-oriented programming language whose syntax is derived from C and OOPS features are derived from C++. It has its syntax, rules, format, and programming paradigm.
In this series, we will learn all the major concepts in Java and programming in detail.
The Java Compiler
This is a compiler for Java programming language and its function is to generate Java class files from the Java source code. Java class file contains a platform-independent Java byte code.
After generating class files, JVM loads these class files and either interprets the byte code or compiles it to machine code using Just-in-time (JIT) compiler.
The Java Virtual Machine (JVM)
JVM is the center of Java programming language and Java platform. The JVM converts the byte code into machine-specific code (also known as object code in other programmer languages).
JVM provides the functionality of garbage collection, memory management, security, etc. JVM is platform-independent and we can customize its functionality using a Virtual interface it provides which is not machine-dependent and is also independent of the operating system.
This platform-independence of JVM allows us to create Java programs on one machine and execute them on another machine (WORA – Write – Once – Run – Anywhere).
The following diagram illustrates the various parts of a JVM.
The JVM is present on the RAM. When the Java program is converted to a class file by the Java compiler, it needs to be executed.
For execution, the class file is brought on the Ram by the class loader. Here the BYTE code is verified for any security issues. Classloader is also responsible for loading, linking and initialization of the program to be executed.
The JVM memory shown in the diagram above contains various components as shown and they help in the execution of the program by holding PC registers that contain current instruction to be executed, Heap which holds variables, arrays, etc.
JVM memory also contains method area and Native method stack which contains method code and native language specification respectively. JVM stack holds the instance of the program or the current thread. If there is more than one thread than each thread will have its JVM stack.
Once the program is properly linked, then it is passed on to the execution engine. The execution engine cooperates with the native method interface and native method libraries. Native method interface is a programming framework that allows Java code to call libraries and native applications that are a part of the native method libraries.
Next, the execution engine converts the Bytecode into Native machine code using just-in-time (JIT) compiler. Then the program is executed by the machine.
Note: JIT or Just-in-time compiler is a part of the Java Virtual Machine (JVM). It interprets a part of the Byte Code that has similar functionality at the same time. In other words, it converts the byte code into native machine code at the same programming level. This is the reason for which Java is compiled as well as an interpreted language.
The Java Runtime Environment (JRE)
JRE, as the name suggests, is the runtime environment that is required to execute Java programs and applications. JRE consists of Java Virtual Machine (JVM) and binaries and other classes to successfully execute Java programs.
JRE is a subset of JDK and doesn’t contain any development tools such as Java compiler, debugger, etc. Hence if your Java applications are developed elsewhere, and you need to only execute them on your machine, then you can install JRE separately. You do need to install JDK for this.
You need a JRE installed on your machine as it’s the minimum requirement to run Java programs on your machine.
The JRE includes the following components.
- Code libraries, property settings, and resource files: These include files like charsets.jar, rt.jar, etc.
- DLL files: Used by Java hotspot client virtual machine and server virtual machine.
- Java extension files: For Example, files related to locale specification like localedata.jar
- Files required for security management. For Example, java.policy, java.security
- Applet support classes.
- True Type font files: Usually required by the platform.
To execute any application/program written in Java, you need JRE installed on your system. JRE is platform dependent. This means you need to download and install JRE that’s compatible with your O.S and architecture.
The Java Development Kit (JDK)
This is the core component of any Java environment. JDK contains JRE (Java Runtime Environment) along with Java compiler, Java debugger, and other core classes. JDK is used for Java development as it provides the entire executable and binaries as well as tools required to compile, debug a Java program.
JDK is a platform-specific software and thus we will have separate JDK installers for each Operating system.
JDK contains the following components:
- jConsole: This is a Java monitoring and management Console.
- jar: This is the archiver. This tool is used to package related class libraries into a single Jar file as well as to manage Jar files.
- jarSigner: This tool is used for jar signing and verifying.
- javap: This is a tool for class file disassembler.
- javaws: Java web start launcher for JNLP applications.
- jhat: Java heap analysis tool.
- jrunscript: Java command-line script shell.
- jstack: Utility used to print stack traces for Java threads.
- Javadoc: This automatically generates documentation from the source code comments.
- appletviewer: Used for applet execution and debugging without a web browser.
- apt: Annotation processing tool.
- extCheck: Utility used to check jar file conflicts.
- keytool: Using this utility you can manipulate Keystore.
- policytool: This is a policy creation and management tool.
- xjc: This is a part of the XML binding (JAXB) API that accepts XML schema and generates Java classes.
Thus you can see that the components of JDK are the ones that are required by us from a development point of view.
As shown in the above diagram, the Java source code is converted to byte codes by Java compiler which is a part of JDK. Then this byte code is passed on to JVM (this process already described above) and from there it goes to JRE where the code is executed.
Having seen the details about JDK and JRE, now let’s discuss the differences between JRE and JDK.
JRE Vs JDK
|1||JRE stands for Java Runtime Environment.||JDK stands for Java Development Kit.|
|2||Mostly used for the execution of Java programs.||JDK is used by developers for developing Java programs.|
|3||Contains Java Virtual machine (JVM) bundled inside it.||Doesn’t have JVM.|
|4||JRE doesn’t have Java compiler so cannot compile programs.||JDK has javac compiler and is responsible for compiling programs.|
|5||Contains java class library, the java command, and other infrastructure.||JDK contains tools like Javadoc and archiver that are used to develop Java applications.|
|6||JRE can be installed as a standalone program.||JDK is a separate installer and comes bundled with JRE.|
|7||Takes the compiled/interpreted Java program as input and generates output.||Compiled Java source program and generates a class file which is then given to JVM.|
In this tutorial, we discussed the details of the Java Development Kit (JDK) that comes bundled with Java Runtime Environment (JRE) and Java compiler. JRE, in turn, contains Java Virtual Machine (JVM) which is responsible for converting byte code generated by Java compiler into machine-specific code.
All these including Java language are components of a parent entity called Java platform which is an environment that helps us to run a Java application. We also discussed the detailed architecture and working of JVM as well as the execution of a Java program here.