Java's JAVA_HOME, Path, CLASSPATH environment variable summary, can make use of the three configurations to understand the role of Oracle configuration in a few environment variables

Question: Where can execute the java command, and JAVA_HOME variables that are java path with it or path specified in it? ?

 

Just learning Java, a lot of jdk configuration tutorials are required to set JAVA_HOME, Path, CLASSPATH3 variables. The Java official website, it reads: after jdk1.5 version without CLASSPATH variable is set during installation. Today I will take jdk1.5, for example, summarize the difference between the three.

Path
When we finished installing jdk, open cmd (in the path of non-installation directory) input javac, java, can not find the command prompt. We need to add the path where the command to the Path system variable, this time, the system can locate the executable files.

Windows looks for the executable file is in: when the terminal input java, the system will first look in the current directory java program, if there will be the implementation of java, otherwise it will be specified in the Path to find the path, because we PATH Configuring the ... \ jdk \ bin, the system will find the Java program in this path and executed. Otherwise you can not find the command prompt. Path of action is actually to help us with some command.

JAVA_HOME
content inside JAVA_HOME is the JDK installation directory. In fact, do not set JAVA_HOME is also possible, as long as the Path on the line . However, you can easily set the JAVA_HOME use in Path, update the JDK directory. For example, jdk installation path is C: \ jdk1.5.0_22, Path is set to C: \ jdk1.5.0_22 \ bin, and when we JAVA_HOME set to C: \ jdk1.5.0_22, set the Path can be written% JAVA_HOME% \ bin, later when we use other versions of the jdk, you can only modify the value of JAVA_HOME, in addition, when we want to use Redis and other components, but also the flexibility to use JAVA_HOME to make some changes.

CLASSPATH
compiler, when you run a Java program, JRE will go to the specified variable path in search of the required class (.class) file, a lot of configuration tutorial let us set the CLASSPATH environment variable:;.% JAVA_HOME% \ lib \ dt. jar;% JAVA_HOME% \ lib \ tools.jar, but in fact it is not necessary. To jdk1.5, for example, when we configured, write a HelloWorld.java.

 

 Cmd file into the directory, you can find still can compile and run

 

 View details about running, you can see rt.jar without CLASSPATH, the current directory can also be found in the JVM.

 

 That is, after JDK 1.5, can not configure this variable. Not configured CLASSPATH, JRE will automatically search for class files in the current directory. Compiler, runtime, the system can automatically load and dt.jar tools.jar Java class files.

  Of course, using the above JDK version 1.5 can be arranged in the CLASSPATH environment variable properties, once the variable is set, it will search the JRE Java class (if the current path is not included in the CLASSPATH variable according to the specified path., JRE would not Search Java classes) in the current path. This method is once and for all when we just want to run some temporary category, we they can be temporarily added to the CLASSPATH (cmd closed, gone). As shown in the following:

Returns the parent directory, there is no doubt that the parent directory is not HelloWorld.class files, so can not find the main class runs. This is because the JVM will only find class files in the current directory, dt.jar, tools.jar rather not find class files in subdirectories in the current directory.

 

 The solution is (1) to temporarily add to the CLASSPATH (only take effect in the current window) subdirectory of the current directory with the command set, then you can run:

 

 (2) Also want to specify the path to the temporary Java JRE search class when running a Java program, you can also use the -classpath (-cp) option, as in the following format. (Dir to specify the path name)

java -classpath dir class name

 

java -classpath dir class name

 

 

 

For the second method, there is a point to note: Using this method will first find the class file in the classpath, instant current directory also has a class may not find as follows: 1 Create an empty file named folder, entering helloworld folder, although there helloworld class files would not be able to recognize.

 

 In other words, the use of the -classpath option, JRE will be in strict accordance with the specified -classpath path to search for Java classes. If you want to make CLASSPATH environment variable specifies the path to also take effect, you can press the following format to run Java programs.

java -classpath %CLASSPATH%;.;dir 类名

 

Summary:
1, to find the path environment variable is the first execution of the program in the current directory, and if not, to specify the directory path to look for. The classpath is the first to look in the classpath environment variable execution of the program, found, even if the current directory has the same program execution can not be performed; and as long as after the value of the classpath periods will be added in the current directory to find the execution program. For convenience, later try to use the set CLASSPATH, instead of -classpath option.

2, when you set up a temporary variable, if you want to keep the original value of the path, just behind the new value plus% path% this statement on the line. Such as: set path = new path;% path%;

3, when using javac and java compile and execute the program, if the situation can not find the file appears, set the correct premise, only two possibilities in the environment variable: one directory error; the second is the filename error.
----------------
bloggers' JeremyChan1887 "
Original link: https: //blog.csdn.net/sinat_30973431/article/details/82556821

Guess you like

Origin www.cnblogs.com/isme-zjh/p/11577019.html