02 Environmental Preparation a Journey of a Thousand Miles Begins With a Single Step

02 Environmental Preparation- A Journey of a Thousand Miles Begins with a Single Step #

Writing code in Java is very simple and easy to learn, making it suitable for developing various enterprise applications and business systems. It is a well-known fact that the simpler a system is to use, the more complex the principles and implementations behind it. This is easy to understand, as the internal implementation of a system takes into account various extreme scenarios while hiding complexity from the user. As the cornerstone of the vast Java ecosystem, the internal implementation of the JVM (Java Virtual Machine) is very complex. According to statistics, the implementation code of OpenJDK has exceeded 10 million lines.

Is JVM difficult or not? Naturally, “it’s easy for those who know, and difficult for those who don’t”. A towering skyscraper starts from the ground, and without mastering the basic knowledge, various principles learned and related techniques understood may be forgotten in an instant or regretted for not having enough knowledge when needed.

Mastering the basic knowledge, learning and practicing regularly, and using various tools proficiently, will naturally enable a deep understanding of a skill. Combining theory with practice, mastering JVM-related knowledge, and becoming proficient in the use of various tools are indispensable in the career advancement of a Java engineer. Learning involves understanding theory and mastering implementation principles. By understanding the JVM of the Java standard platform, it becomes easier to understand Android’s ART, Go’s virtual machine, and garbage collection implementations of various languages by making slight adaptations.

1.1 Relationship among JDK, JRE, and JVM #

JDK

JDK (Java Development Kit) is a software development toolset used to develop Java applications. It includes the Java runtime environment (JRE), interpreter (Java), compiler (javac), Java archive (jar), documentation generator (Javadoc), and other tools. In simple terms, if we want to develop Java programs, we need to install a certain version of the JDK.

JRE

JRE (Java Runtime Environment) provides the environment needed to execute Java applications. It includes the Java Virtual Machine (JVM), core classes, supporting files, etc. In simple terms, if we want to run Java programs on a machine, we can install either the JDK or just the JRE, with the latter having a smaller size.

JVM

Java Virtual Machine has three meanings:

  • JVM specification requirements.
  • A specific implementation (a computer program) that meets the JVM specification requirements.
  • A running instance of a JVM. When writing Java commands to run Java classes at the command prompt, a JVM instance is created. If we only mention “JVM” below, it refers to this meaning; if we mention a specific JVM name, such as Zing JVM, it indicates the second meaning above.

Relationship among JDK, JRE, and JVM

In terms of scope, JDK > JRE > JVM:

  • JDK = JRE + Development tools
  • JRE = JVM + Libraries

0.18346271077222331.png

The interaction among these three when developing and running Java programs:

In simple terms, programs developed using JDK can be compiled, packaged, and distributed to machines with JRE installed for execution. The running program is an instance of JVM created by running the Java command. The execution of the code logic runs on this JVM instance.

0.9484384203409852.png

The development and running process of Java programs are as follows:

We use the JDK (calling Java API) to develop Java programs, which are then compiled into bytecode or packaged programs. Then, using the JRE, we start a JVM instance, which loads, verifies, and executes Java bytecode and dependencies, running the Java program. The JVM interprets the Java bytecode of the program and its dependencies and converts it into native code for execution, producing results.

1.2 Development and Versions of JDK #

After discussing these JDK-related concepts, let’s take a look at the development and versions of JDK. List of JDK versions

JDK Version Release Date Codename Notes
1 January 23, 1996 Oak The first version, a great milestone, but purely interpreted and uses JIT, with poor performance and slow speed.
1.1 February 19, 1997 Sparkler JDBC, support for inner classes, RMI, reflection, and more.
1.2 8th December 1998 Playground Collection framework, JIT, etc.
1.3 8th May 2000 Kestrel Significant optimizations and enhancements to various aspects of Java.
1.4 6th February 2004 Merlin XML processing, support for IPV6, regular expressions, introduction of nio and CMS garbage collector.
5 30th September 2004 Tiger Generics, enhanced for loop, autoboxing and unboxing, varargs, static import, annotations.
6 11th December 2006 Mustang Support for scripting languages, JDBC 4.0.
7 28th July 2011 Dolphin Switch statement supports String type, type inference for generics, nio 2.0 APIs, binary literals for numeric types.
8 18th March 2014 Spider Lambda expressions, default methods in interfaces, Stream API, new date and time API, Nashorn engine jjs, introduction of G1 garbage collector.
9 22nd September 2017 Modularity Module system, HTTP 2 client, multi-version JARs, private interface methods, improved Stream API, Reactive Streams API.
10 21st March 2018 Introduces the var keyword for local variable type inference, unified garbage collection interface.
11 25th September 2018 HTTP client (Standard), no-op garbage collector, support for ZGC garbage collector, first LTS version.
12 19th March 2019 Introduces a new garbage collector called Shenandoah, enhanced switch statement, improvements to G1 garbage collector.
13 17th September 2019 Improvements to CDS memory sharing, return of memory in ZGC, enhancements to Socket API and switch statement, introduction of text blocks.
14 In development Continued improvements to ZGC and G1, marks the ParallelScavenge + SerialOld combination as deprecated, removal of CMS garbage collector.

Java Highlights

  1. 23rd May 1995 - Birth of Java language
  2. January 1996 - First JDK, JDK1.0, released
  3. 18th February 1997 - JDK1.1 released
  4. 2nd April 1997 - JavaOne conference held with over 10,000 attendees, setting a record for the largest global conference at the time
  5. September 1997 - Java developer community surpasses 100,000 members
  6. February 1998 - JDK1.1 downloaded over 2 million times
  7. 8th December 1998 - JAVA2 Enterprise Platform J2EE released
  8. June 1999 - Sun releases three versions of Java: Standard Edition (J2SE), Enterprise Edition (J2EE), and Micro Edition (J2ME)
  9. 8th May 2000 - JDK1.3 released
  10. 29th May 2000 - JDK1.4 released
  11. 26th February 2002 - J2SE1.4 released, significantly increasing Java’s computational capabilities
  12. 30th September 2004 - J2SE1.5 released, a milestone in the history of Java language development, introduction of the Java Util Concurrency (JUC) package. Renamed as J2SE5.0 to reflect its importance.
  13. June 2005 - Java SE 6 released, a long-term supported version.
  14. On November 13, 2006, Sun announced that Java adopted GNU General Public License Version 2, thus making the source code of Java open to the public.
  15. On April 20, 2009, Oracle acquired Sun for $7.4 billion and gained the copyright of Java.
  16. On July 28, 2011, Oracle released the official version of Java SE 7.0.
  17. On March 18, 2014, Oracle released Java SE 8, which is currently the most widely used version.
  18. On September 22, 2017, JDK 9 was released. The API underwent major adjustments and support for WebSocket and HTTP/2 was added. Since then, a major version has been released every six months.
  19. On March 21, 2018, JDK 10 was released. The biggest change is the introduction of ‘var’, which you would know if you are familiar with C# or JavaScript/NodeJS.
  20. On September 25, 2018, JDK 11 was released, introducing ZGC. This is also the first long-term maintenance version LTS that has been announced.
  21. On March 19, 2019, JDK 12 was released, introducing the Shenandoah GC with millisecond-level pauses.
  22. On September 17, 2019, JDK 13 was released, improving CDS memory sharing and returning ZGC to the system.

We can see that JDK has been developing more and more, becoming more and more complex. Especially after being acquired by Oracle, the version number has rapidly inflated in the past two years, and the GC algorithm has also seen faster development. The latest JDK is currently JDK 13, while JDK 14 is under development and is expected to be released in March 2020. Many friends have exclaimed, “Stop upgrading, I’m still using JDK 8 and can’t keep up.” But it is precisely because of Java’s continuous development and improvement that it continues to have vitality.

The regular JDK usually refers to OpenJDK or Oracle JDK. Of course, Oracle also has a new JVM called GraalVM, which is also very interesting. In addition to Sun/Oracle’s JDK, there are other versions such as JRockit from the former BEA (now acquired by Oracle), J9 from IBM, Zing JVM from Azul, and DragonWell, a branch version from Alibaba.

1.3 Installing JDK #

The JDK is usually downloaded from the Oracle official website. Open the page and scroll to the bottom to find “Java for Developers” or “Developers”. Go to the corresponding Java page or Java SE page, find the “Download” button, accept the license agreement, and download the corresponding x64 version. 891e2fe6-e872-4aa9-b00d-d176e947f11f.jpg

It is recommended to install a relatively new version of JDK 8, such as JDK 8u231. 3bbdc5e9-149c-407d-b757-69a061581aae.png

Note: Registering and logging in to an Oracle account is required to install the JDK from the Oracle official website. Nowadays, it is common to place the download links at the bottom of the page, and many tools do the same. Currently, it is recommended to download JDK 8. In the future, JDK 11 may become the mainstream version, as Java 11 is an LTS long-term support version. However, it may take some time to popularize, and the directory structure of JDK 11 is different from before, so many tools may not be compatible with its file structure.

Some operating systems provide automatic installation tools, which can be used directly. For example, using yum, brew, apt, etc. For example, on a MacBook, you can execute:

brew cask install java8

And using the following command will install the latest JDK 13 by default.

brew cask install java

If you have 360 Software Manager or Tencent Software Manager on your computer, you can directly search and download the JDK (the version may not be the latest, but you don’t need to register and log in to an Oracle account): 035a0b3e-de33-4e97-946c-c9adb8b68ae7.png

If the network is not good, you can get it from my Baidu Netdisk:

https://pan.baidu.com/s/16WmRDZSiBD7a2PMjhSiGJw

Extraction code: e77s

1.4 Setting environment variables #

If the command cannot be found, you need to set the environment variables: JAVA_HOME and PATH.

The JAVA_HOME environment variable represents the installation directory of the JDK. By modifying JAVA_HOME, you can quickly switch JDK versions. Many tools depend on this environment variable.

In addition, it is recommended not to set the CLASS_PATH environment variable. It is not necessary for beginners and can cause some troubles.

For Windows systems, go to System Properties - Advanced - Environment Variables to set system environment variables. If you don’t have permission, you can only set user environment variables.

For Linux and macOS systems, you need to configure scripts. For example:

$ cat ~/.bash_profile

# JAVA ENV
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home
export PATH=$PATH:$JAVA_HOME/bin

Make the environment configuration take effect immediately:

$ source ~/.bash_profile

View environment variables:

echo $PATH
echo $JAVA_HOME

Generally, scripts like .bash_profile are only used to set environment variables. Do not set programs that start randomly with the machine.

What if you don’t know where the JDK installed by automatic installation/others is located?

The simplest/most troublesome way to search is to ask the relevant personnel.

There are many ways to search, such as using which, whereis, ls -l to track symbolic links, or using the find command to globally search (may require sudo privileges), for example:

jps -v
whereis javac
ls -l /usr/bin/javac
find / -name javac

Find the path that satisfies $JAVA_HOME/bin/javac.

For Windows systems, where it is installed is where it is, usually under C:\Program Files (x86)\Java. You can also view the path of a program through Task Manager. Note that JAVA_HOME cannot be in the C:\Windows\System32 directory.

Then we can see many JVM tools under the JDK installation path, such as on macOS:

54940291.png In the subsequent chapters, we will detail the usage of some of these tools and how to use them to analyze the JVM.

1.4 Verify JDK installation #

After the installation is completed, the Java environment can generally be used. The script command to verify it is:

$ java -version

You can see output similar to the following, which indicates that the installation is successful:

java version “1.8.065” Java™ SE Runtime Environment (build 1.8.065-b17) Java HotSpot™ 64-Bit Server VM (build 25.65-b01, mixed mode)

Then we can write a simple Java program, create a new text file, and enter the following content:

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

Then change the file name to Hello.java, and execute the following command in the command line:

$ javac Hello.java

Then run it using the following command:

$ java Hello Hello, JVM!

This proves that the execution is successful, and our JDK environment can be used for development.

Reference materials #

  1. https://www.jianshu.com/p/7b99bd132470
  2. https://blog.csdn.net/Phoenix_smf/article/details/79709592
  3. https://www.iteye.com/blog/dasheng-727156
  4. https://blog.csdn.net/lc11535/article/details/99776597
  5. https://blog.csdn.net/damin112/article/details/84634041
  6. https://blog.csdn.net/KamRoseLee/article/details/79440425
  7. https://blog.csdn.net/j3T9Z7H/article/details/94592958
  8. http://openjdk.java.net/projects/jdk/
  9. http://openjdk.java.net/projects/jdk/13/