Professional Documents
Culture Documents
JDK Jre JVM
JDK Jre JVM
com/java/basics/jdk-jre-jvm/
Learn the differences between JDK, JRE and JVM. How JVM works inside? What
are class loaders, interpreter and JIT compilers. Also checkout some interview
questions.
Table of Contents
1. We write the Java source code in Simple.Java file using an editor or IDE
(integrated development environment) e.g. Eclipse or IntelliJ Idea.
2. Program has to be compiled into bytecode. Java compiler (javac) compiles the
sourcecode to Simple.class file.
3. This class file can be executed in any platform/OS by JVM (Java virtual machine).
4. JVM translates bytecode into native machine code which machines can execute.
Java Execution Flow
2. What is JVM?
Java Virtual machine (JVM) is the virtual machine that runs the Java bytecodes. You get
this bytecode by compiling the .java files into .class files. .class files contain the
bytecodes understood by the JVM.
In the real world, JVM is a specification that provides a runtime environment in which
Java bytecode can be executed. Different vendors provide different implementations of
this specification. For example, this wiki page lists down different JVM implementations.
Most popular implementation of JVM is Hotspot which is owned and provided by Oracle
Corporation. (Previously by Sun Microsystems, Inc.).
JVM delivers the optimal performance for Java applications using many advanced
techniques, incorporating a state-of-the-art memory model, garbage collector,
and adaptive optimizer.
JVM comes in two different flavors – client and server. Although the Server and the
Client VMs are similar, the Server VM has been specially tuned to maximize peak
operating speed. It is intended for executing long-running server applications, which
need the fastest possible operating speed more than a fast start-up time or smaller
runtime memory footprint. Developers can choose which system they want by
specifying -client or -server.
The JVM is called virtual because it provides a machine interface that does not depend
on the underlying operating system and machine hardware architecture. This
independence from hardware and the operating system is a cornerstone of the write-
once-run-anywhere value of Java programs.
JVM Architecture
The class loader is a subsystem used for loading class files. It performs three major
functions i.e. class loading, linking, and initialization.
1. Loading
2. Linking
3. Initialization
This is the final phase of class loading, here all static variable will be assigned with
the original values and the static blocks will be executed.
Memory area inside JVM is divided into multiple parts to store specific parts of
application data.
Method Area stores class structures like metadata, the constant runtime pool, and
the code for methods.
Heap stores all objects that are created during application execution.
Stacks store local variables, and intermediate results. All such variables are local to
the thread by which they are created. Each thread has its own JVM stack, created
simultaneously as the thread is created. So all such local variable are
called thread-local variables.
PC register store the physical memory address of the statements which is
currently executing. In Java, each thread has its separate PC register.
Java supports and uses native code as well. Many low level code is written in
languages like C and C++. Native method stacks hold the instruction of native
code.
2.2.1. Interpreter
A JVM interpreter pretty much converts each byte-code instruction to corresponding
native instruction by looking up a predefined JVM-instruction to machine instruction
mapping. It directly executes the bytecode and does not perform any optimization.
The JIT compiler is enabled by default. You can disable the JIT compiler, 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.
3. What is JRE?
The Java Runtime Environment (JRE) is a software package which bundles the libraries
(jars) and the Java Virtual Machine, and other components to run applications written in
the Java. JVM is just a part of JRE distributions.
To execute any Java application, you need JRE installed in the machine. It’s minimum
requirement to execute Java applications on any machine.
JREs can be downloaded as part of JDKs or you can download them separately. JREs are
platform dependent. It means that based on the type of machine (OS and architecture),
you will have to select the JRE bundle to import and install.
For example, you cannot install a 64-bit JRE distribution on 32-bit machine. Similarly,
JRE distribution for Windows will not work in Linux; and vice-versa.
4. What is JDK?
JDK is a superset of JRE. JDK contains everything that JRE has along with development
tools for developing, debugging, and monitoring Java applications. You need JDK when
you need to develop Java applications.
appletviewer – this tool can be used to run and debug Java applets without a web
browser
apt – the annotation-processing tool
extcheck – a utility that detects JAR file conflicts
javadoc – the documentation generator, which automatically generates
documentation from source code comments
jar – the archiver, which packages related class libraries into a single JAR file. This
tool also helps manage JAR files
jarsigner – the jar signing and verification tool
javap – the class file disassembler
javaws – the Java Web Start launcher for JNLP applications
JConsole – Java Monitoring and Management Console
jhat – Java Heap Analysis Tool
jrunscript – Java command-line script shell
jstack – utility that prints Java stack traces of Java threads
keytool – tool for manipulating the keystore
policytool – the policy creation and management tool
xjc – Part of the Java API for XML Binding (JAXB) API. It accepts an XML schema
and generates Java classes
Same as JREs, JDKs are also platform dependent. So take care when you download the
JDK package for your machine.
For example, this page lists down all available JDK distributions for Java 8.
JDK 8 Distributions
Similarly, JRE 8 distributions are available in this page.
JRE 8 Distributions
Happy Learning !!
Further reading:
https://www.javaworld.com/article/3347596/jvm-jdk-jre-whats-the-difference.html
By JavaWorld staff and Matthew Tyson
JavaWorld | MARCH 11, 2019 11:57 AM PT
IDG / Oracle / Vasabii / Getty Images
MORE LIKE THIS
Developers new to Java often wonder what differentiates the Java Virtual Machine, the Java
Development Kit, and the Java Runtime Environment. They're also curious how these three
Java platform components work together in Java applications. Finally, developers need to
know how they will interact with each component.
Briefly:
The JVM is the Java platform component that executes your programs.
The JRE creates the JVM and ensures dependencies are available to your programs.
The JDK allows you to create Java programs that can be executed and run by the JVM and JRE.
As a developer, you'll work with the JDK to write your applications and with the JVM to debug
and optimize them, especially for performance. The JRE mostly runs in the background, but
you may use it for application monitoring and memory configuration.
That's the overview, but there's a lot more to know about each platform component. We've
pulled it all together in three quick tutorials.
PART 1:
What is the Java Virtual Machine?
Technically, the JVM is a specification, describing the requirements for building a piece of
software. From a developer's perspective, it's how we run our Java programs. You can choose
from a variety of JVM implementations, and it's important to know how to load and execute
class files using the JVM you choose. You also should know how to check and tune JVM
memory usage. Learn more ...
PART 2:
What is the Java Development Kit?
The JDK is the package of tools you need to develop Java-based software. There isn't just one
JDK, though, and it's important to know how to choose and download the correct JDK for the
type of application you want to create. Learn more ...
Vasabii / Getty Images
PART 3:
What is the Java Runtime Environment?
The JRE is the runtime environment for Java, which means it's a piece of software that is
designed to run Java code. While the JRE mostly runs in the background, it's important to
know how to use JRE flags and switches to configure application memory. Learn more ...
https://www.javaworld.com/article/3272244/what-is-the-jvm-introducing-the-java-virtual-
machine.html
By Matthew Tyson
JavaWorld | MAY 22, 2018 12:32 PM PT
RELATED ARTICLES
JavaWorld / IDG
Figure 1: High-level view of the JVM
Having a technical definition for the JVM is useful, and there's also an everyday way that
software developers think about it. Let's break those down:
Technical definition: The JVM is the specification for a software program that executes code
and provides the runtime environment for that code.
Everyday definition: The JVM is how we run our Java programs. We configure the JVM's
settings and then rely on it to manage program resources during execution.
When developers talk about the JVM, we usually mean the process running on a machine,
especially a server, that represents and controls resource usage for a Java app. Contrast this to
the JVM specification, which describes the requirements for building a program that performs
these tasks.
Garbage collection
Before Java, all program memory was managed by the programmer. In Java, program memory
is managed by the JVM. The JVM manages memory through a process called garbage
collection, which continuously identifies and eliminates unused memory in Java programs.
Garbage collection happens inside a running JVM.
In the early days, Java came under a lot of criticism for not being as "close to the metal" as C++,
and therefore not as fast. The garbage collection process was especially controversial. Since
then, a variety of algorithms and approaches have been proposed and used for garbage
collection. With consistent development and optimization, garbage collection has vastly
improved.
[ Learn Java from beginning concepts to advanced design patterns in this comprehensive 12-
part course! ]
What does 'close to the metal' mean?
When programmers say a programming language or platform is "close to the metal," we mean
the developer is able to programmatically (by writing code) manage an operating system's
memory. In theory, programmers can wring more performance out of our programs by
stipulating how much is used and when to discard it. In most cases, delegating memory
management to a highly refined process like the JVM yields better performance and fewer
errors than doing it yourself.
"To implement the Java virtual machine correctly, you need only be able to read the class file
format and correctly perform the operations specified therein."
"All you have to do is touch the right key at the right time."
So, all the JVM has to do is run Java programs correctly. Sounds simple, might even look
simple from outside, but it is a massive undertaking, especially given the power and flexibility
of the Java language.
Almost all licensed JVM's are created as forks off the OpenJDK and the HotSpot JVM, including
Oracle's licensed JDK. Developers creating a licensed fork off the OpenJDK are often
motivated by the desire to add OS-specific performance improvements. Typically,
you download and install the JVM as a bundled part of a Java Runtime Environment (JRE).
3. A JVM instance
After the JVM spec has been implemented and released as a software product, you may
download and run it as a program. That downloaded program is an instance (or instantiated
version) of the JVM.
Most of the time, when developers talk about "the JVM," we are referring to a JVM instance
running in a software development or production environment. You might say, "Hey Anand,
how much memory is the JVM on that server using?" or, "I can't believe I created a circular call
and a stack overflow error crashed my JVM. What a newbie mistake!"
What is a software specification?
A software specification (or spec) is a human-readable design document that describes how a
software system should operate. The purpose of a specification is to create a clear description
and requirements for engineers to code to.
The Java class loader is the part of the JVM that loads classes into memory and makes them
available for execution. Class loaders use techniques like lazy-loading and caching to make
class loading as efficient as it can be. That said, class loading isn't the epic brain-teaser that
(say) portable runtime memory management is, so the techniques are comparatively simple.
Every Java Virtual Machine includes a class loader. The JVM spec describes standard methods
for querying and manipulating the class loader at runtime, but JVM implementations are
responsible for fulfilling these capabilities. From the developer's perspective, the underlying
class loader mechanisms are typically a black box.
Executing code involves managing access to system resources. The JVM execution engine
stands between the running program--with its demands for file, network and memory
resources--and the operating system, which supplies those resources.
System resources can be divided into two broad categories: memory and everything else.
Recall that the JVM is responsible for disposing of unused memory, and that garbage collection
is the mechanism that does that disposal. The JVM is also responsible for allocating and
maintaining the referential structure that the developer takes for granted. As an example, the
JVM's execution engine is responsible for taking something like the new keyword in Java, and
turning it into an OS-specific request for memory allocation.
Beyond memory, the execution engine manages resources for file system access and network
I/O. Since the JVM is interoperable across operating systems, this is no mean task. In addition
to each application's resource needs, the execution engine must be responsive to each OS
environment. That is how the JVM is able to handle in-the-wild demands.
We could say that James Gosling and Brendan Eich invented modern programming, but
thousands of others have refined and built on their ideas over the following decades. Whereas
the Java Virtual Machine was originally just for Java, today it has evolved to support many
scripting and programming languages, including Scala, Groovy, and Kotlin. Looking forward,
it's hard to see a future where the JVM isn't a prominent part of the development landscape.
By Matthew Tyson
JavaWorld | AUGUST 09, 2018 01:20 PM PT
RELATED ARTICLES
Developers new to Java often confuse the Java Development Kit and the Java Runtime
Environment. The distinction is that the JDK is a package of tools for developing Java-based
software, whereas the JRE is a package of tools for running Java code.
The JRE can be used as a standalone component to simply run Java programs, but it's also part
of the JDK. The JDK requires a JRE because running Java programs is part of developing them.
Figure 1 shows how the JDK fits into the Java application development lifecycle.
Matthew Tyson
Figure 1. High-level view of the JDK
Just as we did with my recent introduction to the Java Virtual Machine, let's consider the
technical and everyday definitions of the JDK:
Technical definition: The JDK is an implementation of the Java platform specification, including
compiler and class libraries.
Everyday definition: The JDK is a software package you download in order to create Java-based
applications.
JDK packages
In addition to choosing your Java version, you will also need to select a Java
package. Packages are Java Development Kits that are targeted for different types of
development. The available packages are Java Enterprise Edition (Java EE), Java Standard
Edition (Java SE), and Java Mobile Edition (Java ME).
[ Learn Java from beginning concepts to advanced design patterns in this comprehensive 12-
part course! ]
Novice developers are sometimes unsure which package is correct for their project. Generally,
each JDK version contains Java SE. If you download Java EE or Java ME, you will get the
standard edition with it. For example, Jave EE is the standard platform with additional tools
useful for enterprise application development such as Enterprise JavaBeans or support for
Object Relational Mapping.
It's also not hard to switch to a different JDK in the future if you find you need to. Don't worry
too much about choosing the correct Java version and JDK package when you are just starting
out.
Matthew Tyson
Figure 2. Available JDK packages
Before you select the Java SE download, take a minute to look at the other options. There's a
lot cooking in the Java kitchen!
About Java EE
You would download a Java EE JDK if you were primarily interested in building Java-based web
applications. The Java EE JDK includes the Java Servlet specification, which supports HTTP
request handling. Every Java EE JDK implementation also requires a container, which is a server
that runs Java EE applications. Glassfish is the Java EE server reference implementation for
Oracle. Other popular implementations are Tomcat and Jetty.
Go ahead and download the Java Standard Edition JDK. Options for that are shown in Figure 3.
Matthew Tyson
Figure 3. Available JDKs for download
Installing the "Development Tools" option gives you the JDK proper. Installing "Source Code"
contains the sources for the public classes in the core Java API. Including this option allows you
to reference the source code when building apps. The third option, "Public JRE," drives home
that the JDK and JRE are separate entities: the public JRE can be used by other programs to
execute Java programs, and can be installed separately from the JDK.
Go ahead and install all three components and accept the defaults for each one. Doing this
means your JDK and JRE will be installed in the default locations for your operating system. On
Windows, that's C:\Program Files\Java, as seen in Figure 4.
Matthew Tyson
Figure 4. Java installed
It's good to have java installed, but what about javac? You'll need this JDK element to
compile your Java files.
Next, add the code from Listing 1, which is a very simple Java program.
Listing 1. Intro.java
You'll use the javac command, which stands for Java compiler. Type the full path to the
command into your command shell, and pass the Intro.java file as a command. On my
system, that looks like Listing 2.
That should result in a successful compile. The javac will not respond with a success message;
it will just output the new file. Any errors will result in console output.
You can run it by typing: java Intro, which will result in Listing 3. Note that you don't include
the .class when typing this command.
C:\Users\mtyson\Documents>java Intro
Welcome to the JDK!
Navigate back to the directory where you placed your Intro.java, and type the command you
see in Listing 4.
C:\Users\mtyson\Documents>"c:\Program Files\Java\jdk-10.0.1\bin\jar.exe" --
create --file intro.jar Intro.class
The -cp switch tells Java to add the jar to the classpath. A .jar file is overkill for this tiny
program, but they're indispensable as programs grow in size and rely on third-party packages.
In Java development, one of the key things the IDE does is manage compilation for you. That
is, the IDE automatically runs the compile process in the background so you don't have to
continually do it yourself. An IDE also provides play-by-play feedback as you go, catching
coding errors on the fly.
Several solid IDEs exist for Java. You've seen how the JDK works on the command-line, so now
Let's take a quick look at how it works in the Eclipse IDE.
Eclipse and the JDK
Installing Eclipse is outside the scope of this guide, but it's a simple process. Eclipse includes an
installer like any other program, and you can find the right installer for your operating
system here.
With Eclipse installed, open the Window menu item from the menu bar and
select preferences.
Inside the preferences window, you'll see the Java item. Open it, and inside you'll see
the Compiler item. Clicking that will reveal some options for the JDK.
Matthew Tyson
Figure 5. Eclipse JDK options
As previously mentioned, you will need to select the correct JDK version for your project.
Under the hood, the IDE will run the JDK compiler, just as you ran it from the command line.
The Eclipse IDE also has its own JDK instance. The IDE manages the JDK and JRE for you,
which makes life much easier!
Conclusion
This article is the second in a short series introducing three core Java platform components:
the JVM, JDK, and JRE. Look for the next article in the series, where you'll learn all about the
Java Runtime Environment.
By Matthew Tyson
JavaWorld | SEPTEMBER 11, 2018 01:08 PM PT
IDG / Oracle / Vasabii / Getty Images
MORE LIKE THIS
RELATED ARTICLES
Practically speaking, a runtime environment is a piece of software that is designed to run other
software. As the runtime environment for Java, the JRE contains the Java class libraries, the
Java class loader, and the Java Virtual Machine. In this system:
The class loader is responsible for correctly loading classes and connecting them with the core
Java class libraries.
The JVM is responsible for ensuring Java applications have the resources they need to run and
perform well in your device or cloud environment.
The JRE is mainly a container for those other components, and is responsible for orchestrating
their activities.
We'll dig a lot deeper into how these components work together in the sections that follow.
In short, the JRE is a sort of meta-OS for Java programs. It's a classic example of abstraction,
abstracting the underlying operating system into a consistent platform for running Java
applications.
[ Learn Java from beginning concepts to advanced design patterns in this comprehensive 12-
part course! ]
How the JRE works with the JVM
A Java Virtual Machine is a running software system responsible for executing live Java
programs. The JRE is the on-disk system that takes your Java code, combines it with the
necessary libraries, and starts the JVM to execute it.
The JRE contains libraries and software that your Java programs need to run. As an example,
the Java class loader is part of the Java Runtime Environment. This important piece of software
loads compiled Java code into memory and connects the code to the appropriate Java class
libraries.
In the layered view I just described, the JVM is created by the JRE. From a package perspective,
the JRE contains the JVM, as Figure 1 shows.
Matthew Tyson
Figure 1. The JRE contains the JVM
In most cases, your computer will come with Java installed, and the JRE will be included with
that. If you do ever need to manually install or upgrade, you can download the current JRE
version from Oracle.
JRE versions
The Java Runtime Environment is updated for each new version of Java, and its version
numbers align with the Java platform versioning system, so for example JRE 1.8 runs Java 8.
While you have a variety of JDK packages to choose from (such as Enterprise Edition or
Standard Edition) that isn't the case with the JRE. Most computers run a JRE developed for
Java SE, which is able to run any Java application regardless of how it was developed. Most
mobile devices come with a JRE for Java ME, which is pre-installed on the mobile device and is
not available for download.
Once the JRE is installed, you can interact with it on the command-line by entering java -
version, which will tell you what version is installed. On POSIX systems, you can always check
the installed location with which java.
Basically, the JRE provides the "knobs" you would use to configure and control the
characteristics of a Java application. Memory usage is a prime example, the bread and butter
of systems administration. While memory usage is always important, it's vital in cloud
configurations, and devops is a cloud-based technology. If you're working in a devops
environment, or interested in branching out into devops, it's a good idea to understand how
Java memory works and how it's monitored in the JRE.
Devops or sysadmin?
Devops is a new term, but it describes something that's been true for decades, which is the
interoperability between development and operations. In this sense, devops is just a newer
term for what used to be called operations or systems administration. Like sysadmin, an
important aspect of devops is administering the systems necessary to execute software.
Managing the JRE is a part of managing systems that run Java applications.
The other memory options, heap and stack, remain the same in Java 8.
You use the -Xss switch to configure the stack starting size. The stack then grows dynamically
according to the program's needs.
In between these options are visual profilers like VisualVM that allow for inspecting a running
JVM. These tools enable tracking down hotspots and memory leaks, as well as watching overall
memory consumption in your system.
Conclusion
The Java Runtime Environment is the on-disk program that loads Java applications for the JVM
to execute. A JRE is included by default when you download the Java Development Kit, and
each JRE includes the core Java class libraries, a Java class loader, and a Java Virtual Machine.
It's helpful to understand how the JVM, JDK and JRE interact, especially for working in cloud
and devops environments. In these environments, the JRE takes a stronger role in monitoring
and configuration than it would in traditional Java application development.
https://www.javacodegeeks.com/2018/04/jvm-architecture-overview-of-jvm-and-jvm-
architecture.html
Hello readers! In this tutorial, we will understand and learn the Java Virtual Machine
(JVM) and its architecture. This tutorial will help you to correctly answer the below
questions:
What is JVM in Java?
Different components of JVM
Difference between JVM, JRE, and JDK
1. Introduction
Java Virtual Machine (JVM) is an abstract virtual machine that resides on your computer
and provides a runtime execution environment for the Java bytecode to get executed.
JVM is available for many hardware and software platforms but few Java developers
know that the Java Runtime Environment (JRE) is the enactment of the Java
Virtual Machine (JVM). JVM analyze the bytecode, interprets it, and execute the same
bytecode to display the output.
The basic function of JVM is to execute the compiled .class files (i.e. the bytecode) and
generate an output. Do note, each operating system has a different JVM, but the
generated bytecode output is same across all operating systems. This means that the
bytecode generated on Windows OS can also run on Linux OS and vice-versa, thus
making Java as a platform independent language.
Fig. 1: An overview of Java Virtual Machine
2. Conclusion
In this tutorial, we had an overview of the Java Virtual Machine (JVM) and its core
components. This tutorial can be summarized as:
JVM is the virtual machine that executes the Java code and produces output by
converting the Java bytecode into the machine language
JIT compiler is a sub-component of the JVM and is used to speed up the
performance and the execution time
Java is slow in execution due to:
Dynamic linking
Run-time conversion of the bytecode to the native machine code
However, these bottleneck performance issues are addressed to an extent in the new
Java Development Kit (JDK) versions. Developers can download the sample code
snippet in the Downloads section.
3. Download the Source Code
This was an overview tutorial of Java Virtual Machine (JVM) and its core components.