For many Java developers, understanding how the JDK & JRE differ is a source of confusion. But knowing when to use each one is essential to develop and run Java applications effectively. In this article you’ll discover why the JDK & JRE exist, their differences & similarities, and practically how to use them.
Let’s begin with a quick overview of the JDK & JRE to get a taste of what they’re about.
JDK (Java Development Kit)
The JDK (Java Development Kit) is a library which you download to your computer to help you develop Java applications.
What kind of library? Whether you use an installer, package manager, or zip file to setup the JDK, you end up with a directory structure like this.
$ ls -l /C/Java/jdk-17.0.2
total 128
drwxr-xr-x 1 Tom 197121 0 Dec 7 21:56 bin/
drwxr-xr-x 1 Tom 197121 0 Dec 7 21:56 conf/
drwxr-xr-x 1 Tom 197121 0 Dec 7 21:56 include/
drwxr-xr-x 1 Tom 197121 0 Dec 7 21:56 jmods/
drwxr-xr-x 1 Tom 197121 0 Dec 7 21:56 legal/
drwxr-xr-x 1 Tom 197121 0 Dec 7 21:56 lib/
-rw-r--r-- 1 Tom 197121 1241 Dec 7 21:56 release
What the heck is all this stuff? It’s a suite of juicy tools to help you do an awesome job as a Java developer.
The JDK bin directory
For example, in the bin directory are these programs:
-
javac: compiles
.java
source files into binary.class
files in bytecode format -
jar: creates a
.jar
file archive from all your.class
files, for easy distribution -
javadoc: generates HTML documentation from your Java source files
There are in total 35 tools in the JDK bin directory, including JConsole, used to monitor Java applications. Have a look yourself and try running them!
Let’s compile something using the JDK
One of the most important tools in the JDK is javac
. Without it you wouldn’t be able to compile your source code to later run using the JRE (more on that later).
Let’s try compiling this simple HelloWorld.java
class using javac
.
class HelloWorld {
public static void main(String\[\] args) {
System.out.println("Hello world!");
}
}
We just pass the file name to javac
and the compiler spits out a .class
file.
$ ls
HelloWorld.java
$ /C/Java/jdk-17.0.3+7/bin/javac HelloWorld.java
$ ls
HelloWorld.class HelloWorld.java
And what’s so great about a .class
file?
Well, it’s important to know that the JVM (Java Virtual Machine) receives instructions through bytecode (.class
files) and NOT source files (.java
) files. Bytecode has been optimised and compressed specifically for the JVM to run as fast as possible.
We’ll learn more about the JVM later when we use it to run our HelloWorld application.
JDK key facts
-
it’s a set of tools for developing Java applications
-
includes the javac compiler command to generate Java bytecode from source code
-
has many other tools within bin directory (jar, javadoc etc.)
-
install and use it on your development machine
-
also use on Continuous Integration (CI) servers to build Java applications
JRE (Java Runtime Environment)
While the focus of the JDK is on developing Java applications, the JRE (Java Runtime Environment) is all about running them. It’s a library which you install on any machine you need to run Java programs.
What sort of library? Here’s what we see if we list of the contents of the JRE.
$ ls -l /C/Java/jdk-17.0.3+7-jre/
total 76
drwxr-xr-x 1 Tom 197121 0 Apr 20 00:13 bin/
drwxr-xr-x 1 Tom 197121 0 Apr 19 23:04 conf/
drwxr-xr-x 1 Tom 197121 0 Apr 19 23:04 legal/
drwxr-xr-x 1 Tom 197121 0 Apr 19 23:04 lib/
-rw-r--r-- 1 Tom 197121 2439 Apr 19 23:10 NOTICE
-rw-r--r-- 1 Tom 197121 1592 Apr 19 23:10 release
This is the minimum set of files needed to run any Java application. The most important is java.exe
inside the bin directory. Use the java
command to execute Java application .class
files.
When you do this, a JVM starts up to execute statements contained within .class
files. So you can think of JRE as containing the JVM, but more accurately the JRE has the libraries to start the JVM.
What else is in the JRE?
Also in the JRE are many libraries that Java applications may rely on at runtime. You’ll probably be familiar with some of them, for example:
-
java.util.*
packages (e.g.java.util.Map
) -
java.lang.*
packages (e.g.java.lang.Integer
)
But note that the JRE doesn’t include any of the development tools like javac
and jar
. That’s what the JDK is for. The JRE is only for running Java applications.
Let’s run something using the JRE
Let’s put the JRE to the test and get it running something!
Remember that we compiled HelloWorld.java
into HelloWorld.class
earlier using the JDK? Running it in the JRE is as simple as passing the class name to the java
command.
$ /C/Java/jdk-17.0.3+7-jre/bin/java HelloWorld
Hello world!
Awesome! We got the expected output. Every Java application starts here.
But when we run java
there’s quite a lot happening in the background just to print a simple string:
-
a ClassLoader loads
.class
files into the JVM, so it doesn’t need to know about the filesystem -
the JVM executes classes and manages memory areas like the heap and stack
-
eventually the JVM interacts with native operating system libraries to execute commands
So we’ve compiled an application using the JDK and run it with the JRE. Are we all done here?
Hang on! There’s more to this story, since the JDK actually contains a JRE itself…
JDK & JRE similarities
Whaaat! There’s really a JRE inside the JDK? ๐ค
Yes, there are quite a few similarities between the JDK and JRE, so let’s take a closer look.
JDK contains a JRE
When you download and install the Java Development Kit (JDK) not only do you have everything needed to compile Java applications, but also to run them too! Or in other words, the JDK contains the Java Runtime Environment (JRE).
If my name were Venn and not Tom, I’d illustrate the idea like this.
The JDK includes the JRE to make development as simple as possible. We can compile and run applications using one JDK installation, without any other downloads.
Let’s try compiling and running HelloWorld using only the JDK.
$ /C/Java/jdk-17.0.3+7/bin/javac HelloWorld.java
$ /C/Java/jdk-17.0.3+7/bin/java HelloWorld
Hello world!
Why use the JRE?
If the JDK already includes the JRE, wouldn’t you always use the JDK to compile and run applications?
Yes, it’s perfectly valid to install a JDK onto a production server and use it to run Java applications. But there are some special use cases to consider.
-
size: the JRE is less than half the size of the JDK. Remember all those extra JDK development tools? If your target environment is a device with limited storage, install a JRE instead.
-
security: given all the extra tooling packaged in the JDK, it’s more secure and clean not to make it available in production by instead using the JRE
To see for yourself, let’s compare the size of the JDK and JRE.
$ du -hs /C/Java/jdk-17.0.3+7
301M /C/Java/jdk-17.0.3+7
$ du -hs /C/Java/jdk-17.0.3+7-jre
124M /C/Java/jdk-17.0.3+7-jre
Using the diff
command you can also check the differences between the JDK and JRE. From my investigation I see that the JDK contains 52 files that aren’t in the JRE! None of those are required for running Java applications.
Platform dependent
Both the JDK and JRE are platform dependent. That means you can only use a version that’s been built for the specific type of machine you’re using.
The JDK is available for:
-
Windows
-
MacOS
-
Linux
The cool thing about Java is that the bytecode generated by the JDK’s compiler can be run anywhere that a JRE is available. This is the write once, run anywhere ethos of Java.
For example, you can compile Java source code on a Windows machine and run it on Linux. No problem.
JDK & JRE distributions
Java is maintained by Oracle, who offer their own approved JDK. But many other organisations offer JDK builds.
Here’s a summary of some of the most popular, specifically for the current long term support (LTS) Java 17.
JDK Build | Organisation | JRE available | Free commercial use |
---|---|---|---|
Oracle JDK | Oracle | โ | โ (NFTC license) |
Oracle OpenJDK | Oracle | โ | โ (GNU license) |
Amazon Corretto | Amazon | โ (for Amazon Linux 2) | โ (GNU license) |
Eclipse Temurin by Adoptium | Eclipse | โ | โ (Eclipse license) |
Interestingly, even though Oracle used to offer a JRE for earlier Java releases, that’s no longer the case. How come?
jlink signals the end of pre-built JREs
Well, JDK 9 adds a new tool called jlink
to go alongside the new Platform Module System. jlink
lets you build your own minimal JRE with only the required libraries, saving on storage and offering improved performance over the pre-built JREs that came before.
Since it’s possible to build your own JRE, Oracle no longer saw a need to offer one. Other organisations like Amazon and Eclipse realised that some developers do still use pre-built JREs, so continue to offer it.
Conclusion
You now know the main differences between the Java Development Kit (JDK) and the Java Runtime Environment (JRE).
-
JDK includes tools for development such as compiler
-
JRE is for running Java applications only
-
JDK includes JRE for convenience
You also saw practically how to compile code with the JDK and run it with the JRE.
Stop reading Gradle articles like these
This article helps you fix a specific problem, but it doesn't teach you the Gradle fundamentals you need to actually help your team succeed.
Instead, follow a step-by-step process that makes getting started with Gradle easy.