What is the JDK (Java Development Kit)? Why do we need it?

22nd October 2018 4 By Alin Bistrian

Understanding how Java Development Kit works and how “The JDK manipulates .java” classes that you will write it may look trivial, but I believe it is essential to understand them upfront which should not take more than 15 minutes. What is important is that you know you should not try to memorize all this, you can always refer back to this tutorial whenever you need to.

Introduction to JDK (Java Development Kit)

As Java Developers you will build software using a JDK that is why it is essential to understand what is in it before we attempt to develop sophisticated software like Web or Mobile Applications.

If you do not have a JDK installed on your computer or if you are not sure you can find a tutorial that will help you with that   HERE.

 

Why do I need a JDK?

We need the JDK because it contains the necessary tools for developing and running Java Applications.

What does the JDK contain?

The JDK includes the JRE plus command-line development tools such as compiler and debuggers, but for the moment we will introduce only two of them not because the others are not important but because they are not important now, remember Just-In-Time Learning principle.

The two components that we will introduce are:

  1. Java Compiler – used to compile .java source code files into binary .class files.
  2. JRE (Java Runtime Environment) – is the on-disk environment that will run your Java Programs

 

Let’s start with the compiler.

What is the compiler main job? It checks our code for errors and translates our code which we write from human-readable format into binary code because our computer cannot understand/run anything else but binary code. The compiler ability to catch errors very early it is one of the most essential features of Java, and we will discuss it later in great details, but for now, all you need to understand is that compiler will check for errors in your code and will compile it.

What is JRE (Java Runtime Environment)?

The JRE is the on-disk environment that has all the necessary tools to run your Java Applications.

How does a Java Program look like? There is the precompiled classes and classes that you write, and for now, we will have just one class that we will put it into a HelloWorld.java file:


public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");    
    }
}

The above example is the Java code that we wrote in plain text, but the JRE (Java Runtime Environment) will not be able to run it, remember the computer only understands binary code. So what do we need to do? We will have to use the compiler to compile this “.java” file into “.class” file, and we will not get into how to do that now, we will get to that a little bit later when we set up the development environment. For the moment focus on understanding what it is in the JDK.

But how exactly JRE will run our program?

Let’s look a little deeper into JRE’s (Java Runtime Environment) components:

As we can see in the above diagram the JRE has two main parts:

  1. Library of pre-compiled Java Classes
  2. JVM (Java Virtual Machine)

Next, we will explore what these components are and how they work together to run our Java Application.

First, let us look at Libraries. What are these libraries and why do we need them?

Here is what Wikipedia says about the libraries:

“The Java Class Library (JCL) is a set of dynamically loadable libraries that Java applications can call at run time. Because the Java Platform is not dependent on a specific operating system, applications cannot rely on any of the platform-native libraries. Instead, the Java Platform provides a comprehensive set of standard class libraries, containing the functions common to modern operating systems.”

And the simplified version:

Java Libraries together with JVM will communicate your instructions (the instructions that you write in your classes) to the underlying Operating System. And here is another crucial Java feature: the JVM and Libraries will be able to run your program on any operating system, and you will not have to care about how they will do that. Cross-platform compatibility.

What is a JVM (Java Virtual Machine) and why do we need it?

The JVM is the software that will run your program. Here is where all the precompiled java libraries and our simple program the HelloWorld.class file is loaded into memory by the ClassLoder (ClassLoader is not important at the moment) and after everything is loaded our Java Application is up and running.

Few other jobs that JVM has:

  • Will manage your computer resources for your Application.
  • It has automatic memory management and other useful components.

For now, all you need to care is that it will load the classes into memory and will run your java application.

Now I would like to say a few words about the precompiled libraries that we mentioned earlier but first, let us look at the following diagram:

  • Java SE: When most people think of the Java programming language, they think of the Java SE API. Java SE’s API provides the core functionality of the Java programming language (Ex. a Swing Application).
  • Java EE: The Java EE platform is built on top of the Java SE platform. The Java EE platform provides an API and runtime environment for developing and running large-scale, multi-tiered, scalable, reliable, and secure network applications (Ex. Web Application).
  • Java ME: SThe Java ME platform provides an API and a small-footprint virtual machine for running Java programming language applications on small devices, like mobile phones.
  • Java FX: JavaFX is a platform for creating rich internet applications using a lightweight user-interface API

 

Depending on what are your goals you will use one of these libraries, but most probably you will be using in a way or another the Java SE.

Next, I will explain the JAVA_HOME environment variable.