Linux configuration Java environment variables detail procedures

  • 2020-04-01 04:08:01
  • OfStack

Direct implementation process:

Unzip and install the JDK
Enter the directory where the jdk-6u14-linux-i586.bin file is located under the shell terminal.
Execute the command./jdk-6u14-linux-i586.bin and a protocol will appear. Press enter, and when asked if you agree, type yes, enter. A jdk1.6.0_14 directory is generated in the current directory, and you can copy it to any directory.

Two. Need to configure the environment variables
1. PATH environment variable. To specify a command search PATH, when executed under the shell, it looks 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 in the JDK installation directory to the existing PATH variable. The bin directory contains frequently used executables such as javac/ Java /javadoc wait. After setting the PATH variable, you can execute javac/ Java and other tools in any directory.
2. CLASSPATH environment variable. The purpose is to specify the class search path, using classes that are already written, provided they can be found, of course, which is how the JVM looks for classes. We need to set dt.jar and tools.jar in the lib subdirectory of the JDK installation directory to the CLASSPATH, and of course the current directory ". "must also be added to the variable.
3. JAVA_HOME environment variable. It points to the directory where the JDK is installed. Software such as Eclipse/NetBeans/Tomcat searches the JAVA_HOME variable to find and use the installed JDK.

Three. Three ways to configure environment variables

1. Modify /etc/profile
This approach is recommended if your computer is only used for development, because all users' shells have access to these environment variables, which can cause security problems for your system.
, open /etc/profile with a text editor
, add:


export JAVA_HOME=/usr/share/jdk1.6.0_14 
export PATH=$JAVA_HOME/bin:$PATH 
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar 

, login again
・ annotations
A. You want to change /usr/share/jdk1.6.0_14 to your JDK installation directory
B. Under Linux, use the colon ":" to separate paths
C. $PATH / $CLASSPATH / $JAVA_HOME is used to reference the value of the original environment variable
When setting environment variables, be careful not to overwrite the original values
Common mistakes.
D. The current directory ". "in the CLASSPATH cannot be dropped. It is also a common mistake to drop the current directory.
E. export is to export these three variables as global variables.
F. Case must be strictly distinguished.

2. Modify. Bash_profile

This method is more secure, it can control the use of these environment variables to the user level, if you need to give a user permission to use these environment variables, you just need to modify the user's home directory. Bash_profile.
, open the.bash_profile file in the user directory with a text editor
, add: at the end of. Bash_profile


export JAVA_HOME=/usr/share/jdk1.6.0_14 
export PATH=$JAVA_HOME/bin:$PATH 
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar 

, login again

3. Set variables directly under the shell
Do not agree to use this method, because the change of shell, your Settings are invalid, so this method is only temporary use, to use later to have to set again, more troublesome.
Simply execute the following command at the shell terminal:


export JAVA_HOME=/usr/share/jdk1.6.0_14 
export PATH=$JAVA_HOME/bin:$PATH 
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar 

Test the JDK
1. Create a new test.java file with a 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 !"); 
} 
} 

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

Uninstall the JDK
Locate the _uninst subdirectory of the JDK installation directory
Execute the command./uninstall.sh at the shell terminal to uninstall the JDK.

Is not according to the above process we achieved under the Linux configuration of Java environment variables, I hope to help you learn.


Related articles: