Install Java on CentOS, set path


Publication date:December 8, 2020



INFOMARTION > Install Java on CentOS, set path

summary

This is the procedure for installing Java on CentOS and setting up the path. Since Oracle Java has begun to be paid for, we will use OpenJDK to set up. This description assumes CentOS7.

Table of Contents

  1. Setting details
  2. summary

1. Setting details

This section describes the procedure for setting the path from where you download Java.

1-1. Download Materials

Download OpenJDK from the following site

https://openjdk.java.net/

1-2. Thawing and moving materials

Move OpenJDK to the server and unzip it. The following assumes that you have placed OpenJDK under "/var/work".

[root@hostname ~]# tar xzvf /var/work/openjdk-11+28_linux-x64_bin.tar.gz
[root@hostname ~]# mkdir /usr/lib/java
[root@hostname ~]# mv jdk-11 /usr/lib/java/

The above command unzips OpenJDK under "/var/work", creates a "java" folder under "/usr/lib/", and moves the unzipped OpenJDK under "/usr/lib/java/".

1-3. Register commands in alternatives

Alternatives is a literal translation of the word "alternatives," but it is an alternative command. Simply put, it is a mechanism for setting environment variables with shortcut links that can be switched. This is useful if you have multiple Java programs installed in your system, for example, to make it easier to switch between them. This command sets the path to Java.

[root@hostname ~]# alternatives --install /usr/bin/java java /usr/lib/java/jdk-11/bin/java 1
[root@hostname ~]# alternatives --install /usr/bin/javac javac /usr/lib/java/jdk-11/bin/javac 1

The first line of the above command explains

・A shortcut link to "/usr/bin/java" will be created.

・Set the group name to "java". If you have multiple java groups and want to switch between them, you can register multiple group names with "java".

・Set the shortcut destination to "/usr/lib/java/jdk-11/bin/java".

・Assume priority 1.

This will cause the java command to execute

①A shortcut to /usr/bin/java will be invoked. ※Commands such as "mkdir" and "ls" that are usually executed are also stored under "/usr/bin". The "mkdir" and "ls" commands that you normally use without much thought are invoked here.

②The shortcut destination "/etc/alternatives/java" is called. This is also a shortcut. ※The "java" in "/etc/alternatives/java" is the value specified by the group name.

③In addition, the shortcut destination "/usr/lib/java/jdk-11/bin/java" is invoked.

The java command is executed in the sequence of ① through ③ above.

Check to see if the java command is enabled with the following command.

[root@hostname ~]# java -version
openjdk version "xx" yyyy-mm-dd
OpenJDK Runtime Environment xx.x (build xx+xx)
OpenJDK 64-Bit Server VM xx.x (build xx+xx, mixed mode)

If it appears as above, it is OK.

2. summary

We have described the procedure for installing Java and setting the path.

The java command, which we usually use casually, works by being registered by alternatives and so on. Please refer to it when you newly install java.

Thank you very much for watching until the end.




■INFORMATION

Please click here to go to the top page of INFORMATION.


■PROFILE

Please click here to view the profile.


■For inquiries, please contact

For inquiries about the article, please contact us here.