Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money.

1. The JDK installation

If the server does not have the JDK, you can install it on Linux using the package manager. Using binary is also a good choice. I use CentOS 7.

1.1 yumInstall the JDK

$Yum install Java - 1.8.0 comes with - its * - y
Copy the code

2. JAVA_HOMEconfiguration

2.1 looking forJAVA_HOME

As you know, JAVA_HOME is an environment variable that we typically use to locate Java executables such as Java and Javac.

After installing JDK, you need to define JAVA_HOME in the environment (JAVA_HOME is required for configuring hbase environment variables later). Run the following command on the terminal. If JAVA_HOME has been defined in the environment, the preceding command will print it.

$ echo $JAVA_HOME
Copy the code

Nothing is printed, and JAVA_HOME is not defined in the visible environment; Well, we can try:

$ which java
Copy the code

/usr/bin/java is printed out, but this is not very useful because it is just a symbolic link (soft link) and we confirm this by going into the folder and finding Java files:

To untangle this, we’ll use dirName with readlink:

$ dirname $(dirname $(readlink -f $(which javac)))
Copy the code

As shown above, after finding it, go to the destination folder /usr/lib/JVM and use ll to view the current directory.

⭐ we can find outJava - 1.8.0 comes with - > / etc/alternatives/java_sdk_1. 8.0This link, this is it! Note that this is on CentOSJAVA_HOMELocation!

/usr/lib/ jvm/java-1.8.0-openJDK-1.8.0.302.b08-0.el7_9.x86_64: /usr/lib/ jvm/java-1.8.0-openJDK-1.8.0.302.b08-0.el7_9.x86_64: /usr/lib/ jvm/java-1.8.0-openJDK-1.8.0.302.b08-0.el7_9.x86_64 As can be seen from the figure below: /etc/alternatives/ javA_sdk_1.8.0 is actually a soft link (same as Windows shortcuts), The directory is /usr/lib/jvm/java-1.8.0-openJDK-1.8.0.302.b08-0.el7_9.x86_64. So no matter which JAVA_HOME points to, it points to the same directory.

We can trace into the Java executable directory to see:

There is a tutorial at ⭐ that points JAVA_HOME to jRE-1.8.0-OpenJDK-1.8.0.191.B12-0.el7_5.x86_64, but this shortcut points to the JRE directory, which contains Java commands, You can execute Java programs, but there is no javac command to compile Java programs, so you should point to java-1.8.0-openJDK-1.8.0.191.B12-0.el7_5.x86_64 if you just want to run the program. You can point to jRE-1.8.0-OpenJDK-1.8.0.191.B12-0.el7_5.x86_64.

📚 For Windows, macOS, or Java to find JAVA_HOME, refer to this article (in great detail) : www.jdon.com/51502

2.2 Configuring Environment Variables

As mentioned above, we found the target folder that JAVA_HOME should point to, which is the Java executable folder. Next, let’s start configuring the environment variables.

🔥 The purpose of setting environment variables is that Java commands are not system commands of the system, so you need to configure them so that the system can call Java commands anywhere. (In short, you specify a directory in which programs will look for files when running the software.)

🔥 The most useful function of setting variables for ordinary people is that there is no need to copy some DLL files into the system directory, and the system variable path is a series of paths for the system to search for DLL files.

Linux environment variable startup process:

⭐ If only the current user needs to use Java, you need to edit the ~/. Bashrc file, add JAVA_HOME to the file as the JDK path, save the file and exit (:wq) :

$ vi ~/.bashrc- export JAVA_HOME = / usr/lib/JVM/Java -- 1.8.0 comes with its - 1.8.0.191. B12-0. El7_5. X86_64Copy the code

⭐ If you want Java to be available to all users (recommended), you need to modify the /etc/profile file to add JAVA_HOME:

[root@localhost]$ vi /etc/profile
Copy the code

After entering the Vim editor, add the following content at the end of the file, save and exit:

Export JAVA_HOME=/usr/lib/ JVM/java-1.8.0-openJDK-1.8.0.191.B12-0.el7_5.x86_64 export JRE_HOME=${JAVA_HOME}/ JRE export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib export PATH=${JAVA_HOME}/bin:$PATHCopy the code

The 🔥 export command provides a group of environment variables when a program is executed in shell. Export can add, modify, or delete environment variables for subsequent programs. However, the effect of export execution is limited to this user login operation. If you use export to configure environment variables in the /etc/profile file, the environment variables take effect globally (you do not need to configure them after the next login).

Do not rejoice too soon after configuration, be sure to execute the following command/etc/profileFile configuration takes effect (space between. And /etc/profile)!!

$. /etc/profile
Copy the code

3. Verify the configuration

Now comes the exciting part of verifying the installation and verifying that the configuration is successful!

1) Verify that Java is successfully installed

$ java -version
Copy the code

2) Verify JAVA_HOME

$ echo $JAVA_HOME
Copy the code

3) Verify that the JDK is installed successfully

$ javac
Copy the code

OHHHHHHHHHHHH! Success!

We hope this article will help you 🧠 feel free to leave your thoughts in the comments 🌊, we will discuss and share 🔥