What is in the box?
The JDK contains the programs and libraries necessary to compile your source code into bytecode and then execute your code in the JVM program. It also includes numerous tools that support your work as a developer.
There is a second packaging of Java, called the Java Runtime Edition (JRE). This smaller package only contains the components necessary to run Java bytecode and not the Java compiler.
Java 9 introduced a new way to package Java applications that rendered the JRE superfluous. As of Java 11, Oracle no longer distributes a JRE for both their distribution and the OpenJDK. Certain distributions from other companies may still provide a JRE for the current versions of Java. We will look at the modular approach to packaging Java applications in a later chapter.
The installation of Java will take up approximately 300 MB of disk space depending on the underlying OS.
The following are the directory structures from a Linux and Windows Java installation. The first is for Ubuntu but will be almost identical on all Linux and macOS installations.
The directory structure for Ubuntu 20.04.4 LTS is as follows:
$ ls -g -G total 36 -rw-r--r-- 1 2439 Apr 19 17:34 NOTICE drwxr-xr-x 2 4096 Apr 19 17:34 bin drwxr-xr-x 5 4096 Apr 19 17:33 conf drwxr-xr-x 3 4096 Apr 19 17:33 include drwxr-xr-x 2 4096 Apr 19 17:33 jmods drwxr-xr-x 72 4096 Apr 19 17:33 legal drwxr-xr-x 5 4096 Apr 19 17:34 lib drwxr-xr-x 3 4096 Apr 19 17:33 man -rw-r--r-- 1 1555 Apr 19 17:34 release
The directory structure for Windows Enterprise 11 Version 21H2 is as follows:
>dir 2022-03-29 11:28 AM <DIR> . 2022-05-03 05:41 PM <DIR> .. 2022-03-29 11:28 AM <DIR> bin 2022-03-29 11:28 AM <DIR> conf 2022-03-29 11:28 AM <DIR> include 2022-03-29 11:28 AM <DIR> jmods 2022-03-29 11:28 AM <DIR> legal 2022-03-29 11:28 AM <DIR> lib 2022-03-29 11:28 AM 2,401 NOTICE 2022-03-29 11:28 AM 1,593 release
If we investigate the bin
folder, we will find several executable programs that Java refers to as its tools. On a Windows system, they all have the .exe
extension; on Linux and macOS, they appear as names only. In this chapter, we will discuss the following tools:
jar
java
javadoc
jlink
jmod
jpackage
jshell
javaw
These are tools we will be using in the coming chapters. See the tool specification link in the Further reading section for details on all the tools included in the JDK.
We have divided these tools into the following categories:
- Compiling and executing a Java program
- Assembling and packaging a Java application
- Documenting Java classes
- Read, Evaluate, Print, and Loop (REPL)
Let’s look at each of these categories.
Compiling and executing a Java program
These are the tools that take us from source code to running a Java program. Some important tools are as follows.
javac
This is the Java compiler. Its role is to compile a Java source code file that ends in .java
into a bytecode file that ends in .class
.
java or javaw.exe
This is the tool that starts up the JVM process and then executes the bytecode file in the process. When using java
, a console window will open and remain open until the JVM process ends. The javaw
tool also starts up the JVM and executes a bytecode program. It will not open a console.
Windows users typically do not expect a console to open as they may have never seen one or interacted with one. If you wish to create a Windows shortcut to run a Java program, you will use javaw.exe program.class
.
We will examine these three commands in Chapter 2, Code, Compile, and Execute.
Assembling and packaging a Java application
A Java program can be constructed of hundreds, thousands, or even more .class
files. In these cases, it is necessary to assemble all these files along with any supporting files (such as images) into a single file. Some of the tools that do so are the following.
jar
A Java program or library usually consists of multiple .class
files. To simplify the delivery of such programs, the jar
tool combines all the class files of an application or library into a single file that uses ZIP compression and has the .jar
extension. A .jar
file can be designated as executable. In this case, you already have a Java JDK or JRE installed. We will see how this tool is used in Chapter 2,Code, Compile, and Execute.
jmod and jlink
Java 9 introduced the concept of modular Java, an approach to assembling Java applications that uses a new format for combining class files called .jmod
files. These files are like .jar
files in that they are ZIP compressed files. The jmod
tool creates .jmod
files.
Until Java 9 came along, a single file called rt.jar
held all the Java libraries. Starting with Java 9, the Java libraries exist as individual .jmod
files. Java packages the JVM file as a .jmod
file. What this means to a developer is that it is possible to distribute Java applications that include the JVM and only those components of Java that must be available to execute your program. It is now unnecessary to have the JDK or JRE pre-installed as all that you need to execute the program is in the archive. You still need the jar
tool to construct such an executable as you cannot execute .jmod
files. We will see how these two tools are used in Chapter 16, Deploying Java in Standalone Packages and Containers.
jpackage
The jpackage
tool creates native applications that hold a Java application and a Java runtime. It is used with either .jar
or .jmod
files. The output is an executable file, such as .msi
or .exe
for Windows, or a .dmg
file for a macOS system. We will see how this tool is used in Chapter 16, Deploying Java in Standalone Packages and Containers.
Documenting Java classes
Having carefully documented your code, Java has a tool for gathering all your comments for easy access for other developers who may use your code.
javadoc
Documenting code has always been an issue in almost every language. Java takes a unique approach to encouraging documentation. If you comment your code in a specific format (which we will examine in Chapter 4, Language Fundamentals – Data Types and Variables), the javadoc
tool will generate an HTML page for every class that you create. On this page, you will find all public members of the class presented.
Look at the javadoc
page for the ArrayList
class at https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/ArrayList.html. Everything you see on this web page was written into the source code file that was then converted to the HTML page you are looking at. We will examine this tool in Chapter 11, Documentation and Logging.
REPL
A REPL tool is one that supports the execution of code, one line at a time.
jshell
The jshell
tool allows you to write and execute individual Java statements without the need for the usual decorations of classes and methods. This can be quite useful for learning Java. It can execute code line by line as you write it. We will examine jshell
in Chapter 2, Code, Compile, and Execute.