How does the Java virtual machine set the CLASSPATH environment variable?

The CLASSPATH environment variable is used to save the path of a series of class packages, and it is viewed and configured in exactly the same way as the PATH environment variable. When the Java virtual machine needs to run a class, it will look for the required .class files and class packages under the path defined by the CLASSPATH environment variable.

In order for the Java virtual machine to find the required class file, it is necessary to set the CLASSPATH environment variable and save the HelloWorld.class file path. Execute the following command in the command line window.

set CLASSPATH=C:\Program FilesVava\jdk-11.0.11\bin

After executing the above command, execute the java HelloWorld command again to run the program. Execute the java HelloWorld command to run the program, and the result is shown in the figure below.
1691139704280_environment variable.png1691139704280_environment variable.png

 

As can be seen from the figure above, the Java HelloWorld command runs successfully and outputs the result of "hello world". After setting CLASSPATH in the command window, the program will search for the class file in the specified directory according to the CLASSPATH setting. Therefore, although there is no HelloWorld.class file in the root directory of the C drive, the Java HelloWorld command can still be executed correctly.

 

Guess you like

Origin blog.csdn.net/Blue92120/article/details/132140740