Everyone who has installed JDK knows to set the environment variables after the installation is complete. But why should we set the environment variables? What is the role of environment variables?
1) detailed explanation of path:
After JDK is installed on the computer, the computer cannot run commands such as "javac" and "Java" correctly immediately because the computer does not know where to find these two commands.
How do computers find commands? In Windows, find the Command Based on the path environment variable. The value of the PATH environment variable is a series of paths. The Windows operating system searches for commands in sequence in these paths. If the command can be found, the command is executable; otherwise, the message "'xxx' is not an internal command or an external command, or a program or batch file that can be run" appears. In Linux, the command is searched based on the path environment variable. The value of the PATH environment variable is also a series of paths. Because the Windows operating system is case-insensitive, there is no difference in setting path and path. While the Linux operating system is case-sensitive, there is a difference in setting path and path, therefore, you only need to set the PATH environment variable.
For Windows systems, the system environment variable named path already exists. You can directly modify the environment variable and append it to the value of the environment variable, such as D: \ Java \ jdk1.7.0 \ bin (JDK installation path ). Or create an environment variable named path and set it to D: \ Java \ jdk1.7.0 \ bin
Environment variables in Linux are set through the. bash_profile file. Add/home/Java/jdk1.7.0/bin after the PATH variable of this file (JDK installation path)
Path =.: $ path: $ home/bin:/home/Java/jdk1.7.0/bin
After setting the PATH variable, add the statement for exporting the PATH variable at the end of the. bash_profile file.
Export path
Log on to Linux again or run the following command to make the PATH variable in the file take effect:
Source. bash_profile
2) classpath details:
In fact, if JDK 1.5 or later is used, you do not need to set the classpath environment variable.
What is the role of the classpath environment variable? In JDK versions earlier than 1.4, when the "Java class name" command is used to run Java programs, the computer searches for Java classes based on classpath.
In versions earlier than 1.4, you often need to set the value of the classpath environment variable .; % java_home % \ Lib \ DT. jar; % java_home % \ Lib \ tools. jar (the java_home environment variable should point to the JDK installation directory)
Later, the JRE will automatically search for class files in the current path, automatically load the Java classes in the DT. jar and tools. Jar files, and do not need to set the classpath environment variable. Once this environment variable is set, JRE will only search for the Java class according to the environment variable settings, so the environment variable must be set correctly.
If you want to temporarily specify the JRE search Java class path when running Java, you can use the-classpath option, which is separated by a colon (:) in Linux.
Java-classpath dir1; dir2,...; dirn Java class
Or
Java-classpath %; ..; dir1; dir2,...; dirn Java class