A brief analysis of the role of JAVA_HOME CLASSPATH and PATH

  • 2020-04-01 02:09:51
  • OfStack

1, set the JAVA_HOME:
I. for the convenience of quotation
For example, if your JDK is installed in the C:\ProgramFiles\Java\jdk1.7.0 directory, set JAVA_HOME as the directory path.

Second, the principle of normalization When your JDK path is forced to change, you only need to change the variable value of JAVA_HOME. Otherwise, you will have to change any document that references the JDK directory with an absolute path.

Third party software will reference the agreed JAVA_HOME variable , otherwise, you will not be able to use the software normally, after using JAVA for a long time will know, if a software can not be used normally, you might as well think is this problem.

2. Set CLASSPATH:
This is a very interesting problem, and of course a bit of a pain for beginners. This variable is set so that the program can find the corresponding ".class" file. You to compile A JAVA program - a. ava, get an A.c lass, class files, in the current directory you execute JAVA A, will get the corresponding results (if you have set the CLASSPATH to ". "). Now, you put the A.c lass, moved to another directory (for example: "e: \"), execute the JAVA A, there will be A NoClassDefFindError abnormalities, reason is that I couldn't find it. The class files, now that you have increased the CLASSPATH to: ".. E :\" run Java A in any directory and see what happens ~~:)~~~, everything is fine, the Java command finds the.class file by CLASSPATH!

Classpath =c:\test means that when you run a command, you go to the c:\test folder to find the class file that needs to be executed
Before java5, this path could not specify the current path unless you set classpath=.; C :\test means to find the class file in the current directory first, if you can't find it, go to c:\test; When a variable has more than one value, the values are separated by a semicolon.
Set to find from the current directory (the default after java1.5):set classpath=.

3. Set PATH :(must be set)
The reason is very simple, you want to use %JAVA_HOME%\bin\ Java at any time to execute the Java command, of course not, so you can choose to add %JAVA_HOME%\bin to the PATH, so that we can only use Java to execute the command in any PATH.
When you type your code in the command prompt window, the operating system looks for the application in the current directory and the PATH directory and executes it.


Related articles: