linux simple tutorial on configuring jdk environment variables

  • 2020-06-01 09:41:55
  • OfStack

preparation

window installation VMware
VMware installs the linux system
jdk-8u60-linux-x64.tar.gz
(download link: link: https: / / pan baidu. com s / 1 o88U0wq password: g5d9)

linux decompress command
https://www.ofstack.com/article/103658.htm

linux common command 1: vi command

source command under linux
https://www.ofstack.com/article/103657.htm

Methods/steps

The environment variables that need to be configured

PATH environment variable
The function is to specify the command search path, and when the command is executed under shell, it will look in the path specified by the PATH variable to see if it can find the corresponding command program. We need to add the bin directory under the jdk installation directory to the existing PATH variable. The bin directory contains frequently used executables such as javac/java/javadoc, etc. After setting the PATH variable, we can execute javac/java in any directory.

CLASSPATH environment variable
The function is to specify the class search path, to use the already written class, the premise of course can find them, JVM is CLASSPTH to find the class. We need to set dt.jar and tools.jar in the lib subdirectory of the jdk installation directory to CLASSPATH, and of course, the current directory ". "must also be added to this variable.

JAVA_HOME environment variable
It points to the installation directory of jdk. Eclipse/NetBeans/Tomcat and other software search JAVA_HOME variable to find and use the installed jdk.

【 decompress jdk】

The installation location can be selected according to personal preference. Here we install jdk under /usr/jdk


[chenriyang@RedhatLinux jdk]$ pwd
/usr/jdk
[chenriyang@RedhatLinux jdk]$ ls
jdk-8u60-linux-x64.tar.gz

Unzip jdk - 8 u60 - linux - x64. tar. gz

[chenriyang@RedhatLinux jdk]$ tar -zxvf jdk-8u60-linux-x64.tar.gz

Prompt unzip error


tar: jdk1.8.0_60/release: Cannot open: No such file or directory
tar: Exiting with failure status due to previous errors

The reason is that ordinary users without jdk - 8 u60 - linux - x64. tar. gz operation permissions


[root@RedhatLinux jdk]# ll
total 176992
-rw-r--r--. 1 root root 181238643 Jan 4 16:32 jdk-8u60-linux-x64.tar.gz

We switched to root user, where the password is 123456


[chenriyang@RedhatLinux jdk]$ su root
Password: 
[root@RedhatLinux jdk]# 

Unpack again jdk - 8 u60 - linux - x64. tar. gz

[root@RedhatLinux jdk]# tar -zxvf jdk-8u60-linux-x64.tar.gz

Unpack the success


[root@RedhatLinux jdk]# ls
jdk1.8.0_60 jdk-8u60-linux-x64.tar.gz
[root@RedhatLinux jdk]# 

[modification /etc/profile document]

Note: to ensure that the current user has permission to modify the profile file, simply switch to the root user if you don't want to bother
Open /etc/profile with a text editor

[root@RedhatLinux jdk]# vi /etc/profile

Add at the end of profile file:


#jdk
export JAVA_HOME=/usr/jdk/jdk1.8.0_60
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

Save and exit

:wq
The source command reexecutes the newly modified initialization file profile to take effect immediately without having to log out and log in again

[root@RedhatLinux jdk]# source /etc/profile

annotations
a. Will you/usr/jdk/jdk1. 8.0 _60 instead your jdk installation directory
b. linux USES the colon ":" to separate paths
c.PATH,CLASSPATH, and $JAVA_HOME are used to reference the values of the original environment variable.
The current directory ". "in d.CLASSPATH cannot be thrown away. It is also a common mistake to throw away the current directory.
e. export exports these three variables as global variables.
f. Case must be strictly distinguished.

[test jdk]

1. Create a new Test.java file with the text editor, enter the following code and save it:


public class test { 
 public static void main(String args[]) { 
 System.out.println("A new jdk test !"); 
 } 
} 

Compile: execute the command javac Test.java at the shell terminal
Run: execute the command java Test at the shell terminal
When "A new jdk test!" appears under shell. The word jdk works fine.


Related articles: