Home >Java >javaTutorial >linux configure java environment variables

linux configure java environment variables

高洛峰
高洛峰Original
2016-12-17 13:28:142115browse

Linux configuration java environment variables (details)
1. Unzip and install jdk
Enter the directory where the jdk-6u14-linux-i586.bin file is located in the shell terminal,
Execute the command ./jdk-6u14-linux-i586.bin at this time An agreement will appear. Press Enter continuously. When asked whether you agree, enter yes and press Enter. Afterwards, a jdk1.6.0_14 directory will be generated in the current directory, and you can copy it to any directory.

2. Environment variables that need to be configured
1. PATH environment variable. Its function is to specify the command search path. When executing the command under the shell, it will search in the path specified by the PATH variable to see if the corresponding command program can be found. We need to add the bin directory under the jdk installation directory to the existing PATH variable. The bin directory contains frequently used executable files such as javac/java/javadoc etc. After setting the PATH variable, you can enter it in any directory Execute javac/java and other tools.
2. CLASSPATH environment variable. The function is to specify the class search path. To use the classes that have been written, the prerequisite is of course that they can be found. The JVM uses CLASSPTH to find classes. We need to set dt.jar and tools.jar in the lib subdirectory under the jdk installation directory to CLASSPATH. Of course, the current directory "." must also be added to this variable.
3. JAVA_HOME environment variable. It points to the jdk installation directory. Software such as Eclipse/NetBeans/Tomcat searches for the JAVA_HOME variable to find and use the installed jdk.

3. Three ways to configure environment variables 1. Modify the /etc/profile file
This method is recommended if your computer is only used for development, because all user shells have the right to use these environment variables, which may Bring security issues to the system.
·Open /etc/profile with a text editor
·Add at the end of the profile file:
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

·Log in again
·Notes
a. You need to change /usr/share/jdk1.6.0_14 to your jdk installation directory
b. linux Use a colon ":" to separate the path
c. $PATH / $CLASSPATH / $JAVA_HOME are used to reference the value of the original environment variable
When setting the environment variable, pay special attention not to overwrite the original value. This This is a
common mistake.
d. The current directory "." in CLASSPATH cannot be lost. It is also a common mistake to lose the current directory.
e. export is to export these three variables as global variables.
f. Uppercase and lowercase letters must be strictly distinguished.

2. Modify the .bash_profile file

This method is safer. It can control the permissions to use these environment variables to the user level. If you need to give a certain user permission to use these environment variables, you only need to modify their personal settings. The .bash_profile file in the user's home directory will do.
·Open the .bash_profile file in the user directory with a text editor
·Add at the end of the .bash_profile file:

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

·Log in again

3. Set variables directly under the shell
This method is not recommended, because if you change the shell, your The setting is invalid, so this method is only for temporary use. It will be troublesome to reset it when you want to use it in the future.
Just execute the following command in 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

4. Test jdk
1. Use a text editor to create a new Test.java file, enter the following code in it 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 in the shell terminal
3. Run: Execute the command java Test in the shell terminal
When If "A new jdk test!" appears under the shell, the jdk is running normally.

5. Uninstall jdk
·Find the _uninst subdirectory of the jdk installation directory
·Execute the command ./uninstall.sh in the shell terminal to uninstall jdk.

Desire has no rest.



For more articles related to Linux configuration of java environment variables, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn