-
Book Overview & Buying
-
Table Of Contents
Learn Java 17 Programming - Second Edition
By :
When somebody says “Java”, they may mean quite different things. They could be referring to any of the following:
The following section walks you through the installation of the JDK of Java 17 and the basic related terms and commands.
As we have mentioned already, the JDK includes a Java compiler and the JVM. The task of the compiler is to read a .java file that contains the text of a program written in Java (called source code) and transform (compile) it into bytecode stored in a .class file. The JVM can then read the .class file, interpret the bytecode into binary code, and send it to the operating system for execution. Both the compiler and the JVM have to be invoked explicitly from the command line.
The hierarchy of languages used by Java programs goes like this:
.java file)..class file).Have a look at the following example:
int a = b + c;
When you write the preceding code, the compiler adds the following bytecode to the .class file:
ILOAD b
ILOAD c
IADD
ISTORE a
Write once, run anywhere is the most famous programming marketing jingle driving worldwide adoption. Oracle claims more than 10 million developers use Java, which runs on 13 billion devices. You write Java and compile it into bytecode in .class files. There is a different JVM for Windows, Mac, Unix, Linux, and more, but the same .class file works on all of them.
To support the .java file compilation and its bytecode execution, the JDK installation also includes standard Java libraries called the Java Class Library (JCL). If the program uses a third-party library, it has to be present during compilation and execution. It has to be referred from the same command line that invokes the compiler, and later when the bytecode is executed by the JVM. JCL, on the other hand, does not need to be referred to explicitly. It is assumed that the standard Java libraries reside in the default location of the JDK installation so that the compiler and the JVM know where to find them.
If you do not need to compile a Java program and would like to run only the already compiled .class files, you can download and install the Java Runtime Environment (JRE). For example, it consists of a subset of the JDK and does not include a compiler.
Sometimes, the JDK is referred to as a software development kit (SDK), which is a general name for a collection of software tools and supporting libraries that allow the creation of an executable version of source code written using a certain programming language. So, the JDK is an SDK for Java. This means it is possible to call the JDK an SDK.
You may also hear the terms Java platform and Java edition applied to the JDK. A typical platform is an operating system that allows a software program to be developed and executed. Since the JDK provides its own operating environment, it is called a platform too. An edition is a variation of a Java platform (JDK) assembled for a specific purpose. There are four Java platform editions, as listed here:
All the recently released JDKs are listed on the official Oracle page at https://www.oracle.com/java/technologies/downloads/#java17 (we will call this the installation home page for further references in later chapters).
Here are the steps that need to be followed to install Java SE:
java -version command on your computer displays the correct Java version, as demonstrated in the following example screenshot:
If you follow the installation instructions, you may have noticed a link (Installed Directory Structure of the JDK) given under Table of Contents. This brings you to a page that describes the location of the installed JDK on your computer and the content of each directory of the JDK root directory. The bin directory contains all executables that constitute Java commands, tools, and utilities. If the bin directory is not added to the PATH environment variable automatically, consider doing so manually so that you can launch a Java executable from any directory.
In the previous section, we have already demonstrated the java -version Java command. A list of the other Java executables available (commands, tools, and utilities) can be found in the Java SE documentation (https://www.oracle.com/technetwork/java/javase/documentation/index.html) by clicking the Java Platform Standard Edition Technical Documentation site link, and then the Tools Reference link on the next page. You can learn more about each executable tool by clicking its link.
You can also run each of the listed executables on your computer using one of the following options:
-?, -h, --help, or -help
These will display a brief description of the executable and all its options.
The most important Java commands are listed here:
javac: This reads a .java file, compiles it, and creates one or more corresponding .class files, depending on how many Java classes are defined in the .java file.java: This executes a .class file.These are the commands that make programming possible. Every Java programmer must have a good understanding of their structure and capabilities, but if you are new to Java programming and use an IDE (see the How to install and run an IDE section), you do not need to master these commands immediately. A good IDE hides them from you by compiling a .java file automatically every time you make a change to it. It also provides a graphical element that runs the program every time you click it.
Another very useful Java tool is jcmd. This facilitates communication with, and diagnosis of, any currently running Java processes (JVM) and has many options. But in its simplest form, without any option, it lists all currently running Java processes and their process IDs (PIDs). You can use it to see whether you have runaway Java processes. If you have, you can then kill such a process using the PID provided.