foreword
No matter in the process of daily development or using the computer, we can hardly avoid dealing with environment variables. This is because environment variables play an indispensable role in the operation of the system. As the runtime parameters of the system, some processes are saved. Information required at runtime. As the name suggests, the environment variable is a variable, as a variable, it is used to access data. However, not all data formats can be stored. Generally, strings are stored in the form of key-value pairs, but generally the stored strings are not too long. So from a certain point of view, it is not too much to say that it is a kind of database. As a developer, there should be nothing more familiar than the PATH variable. Install a new software and want it to run everywhere, then you will think of adding it to the environment variable. After all, no one is willing to repeatedly enter such a long file name.
1. Classification of environmental variables
1. System environment variables
Different from user environment variables, it is global and has a wider scope of influence. After the setting takes effect, it can take effect for all users of the system. So it is not recommended to change the system environment variable at will, because it is shared. The modification of this program may affect other programs and cause uncertain problems. Generally, files are used for storage.
Taking the windows system as an example, the system environment variables are stored in the registry at HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SessionManager\Environment , see the figure below
The linux system takes Centos as an example, the system environment variable PATH can be realized by modifying the /etc/profile file or the /etc/environment file
Use the vi /etc/profile command to open the /etc/profile file and append a blank line at the end of the file
export PATH=/app/root2:$PATH
Then you can use source /etc/profile to make it take effect immediately
2. User environment variables
Different from system environment variables, it is relatively local, independent, and has a smaller scope of influence. After the setting takes effect, it only takes effect for the current user. Modifying environment variables may also cause problems if multiple processes share the same environment variables. Taking the Windows system as an example, the system environment variables are stored in the registry at
HKEY_CURRENT_USER\Environment
The linux system takes Centos as an example,
The user environment variable PATH can be implemented by modifying the /.bashrc file
The following uses the vi ~/.bashrc command to open the ~/.bashrc file and append a line at the end of the file
export PATH=/app/root:$PATH
After the modification is completed, you can use source ~/.bashrc to make it take effect immediately. After closing the shell and reopening a shell window, it will also take effect. Use echo $PATH to view the environment variables in the figure below
It should be noted that duplicate paths will be appended when switching login users in the shell
3. Process environment variables
Different from system environment variables and user environment variables, its scope is smaller, and it is only valid within the process. So its isolation is relatively better, and its variables can be considered to be stored in memory.
The linux system takes Centos as an example, and the process environment variables can be set in the shell
It will take effect immediately after the setting is completed
2. The usage and characteristics of environment variables in mainstream programming languages
C#:
Get all environment variables: Environment.GetEnvironmentVariables ();
Obtain system environment variables, user environment variables, and process environment variables respectively
使用Environment.GetEnvironmentVariable
As shown in the figure above, when there are environment variables with the same name in the system environment variables, user environment variables, and process environment variables, how to choose? Obviously, the process environment variables will overwrite the user environment variables, and the user environment variables will overwrite the system environment variables. .
Set system environment variables, user environment variables, process environment variables respectively
使用Environment.SetEnvironmentVariable
Java:
You can use System.getenv to get a single environment variable by name , unfortunately there is no way to set an environment variable.
If you really want to get environment variables, you can only call JNI. in kernel32.dll
SetEnvironmentVariableA and GetEnvironmentVariableA two Windows API functions can be used to set and get the value of environment variables
Call GetEnvironmentVariableA to get the value of the environment variable path
Call Set EnvironmentVariableA to set the value of the environment variable path
Inheritance of environment variables
Because processes exist in a tree structure, there is a parent-child relationship. Generally, when the parent process creates a child process, if the environment variable is not specified, the child process will inherit the environment variable of the parent process.
The following example verifies this
In the above example, an in-process variable inheritvar is set in the parent process, and then a child process based on its own image is started. The command line of the child process has an additional parameter args0. After the child process starts, the process process is read in the child process internal variable inheritvar
The result of the operation is as follows
Explain that the child process inherits the process variables of the parent process by default
3. Application of environment variables
1. Environment variables and batch processing
Environment variables can be said to be the most widely used in the command line and batch processing, and it is very convenient to add and modify environment variables in the command line and batch processing, because the most commonly used function of the command line is to create a subprocess
Use the set command to set or view environment variables, of course, this environment variable belongs to the in-process environment variable
set environment variable name can view environment variables
set environment variable name=environment variable value You can set environment variables, for example, we can set the path environment variable to make the program
ConsoleProcessFormat.exe can be quickly run in other working directories
We used %path% when setting. This is an extended environment variable. In this way, the original value of the existing environment variable path can be referenced. So on the command line, we have two ways to view the value of the environment variable, one is to use the set environment variable name, and the other is to use the extended environment variable
Extended environment variables can use methods to get values in C#
2. The relationship between java.library.path and path environment variable in java
Loading dll in java is generally realized through System.load and System.loadLibrary. These two functions load dynamic libraries
It will check the system properties java.library.path and sun.boot.library.path, and check ClassLoader.loadLibrary.
Then when the java.library.path system property is not explicitly specified in the command line parameters, we can find that java.library.path takes the value of the path environment variable
Then it should be noted that if we explicitly specify java.library.path, if the original path is not added, it is likely that some basic dependent libraries of the system cannot be found. Therefore, when manually specifying java.library.path, it is best to add the value of the original path environment variable.
The reason why setting the java.library.path property in the process does not take effect
Sometimes we want to set the value of java.library.path through System.setProperty in the process to dynamically add the search path of the dll file, but it will not take effect. Tracing back to the source, it was found that it was the cause of the cache.
System.load will call the loadLibrary method of the ClassLoader class. Part of the code for the loadlibrary method is as follows
After the java process starts, the static attribute sys_paths of the ClassLoader class has been assigned, so the default usr_paths takes the default java.library.path attribute value
So if you want the java.library.path you set to take effect, you need to reset the value of sys_paths to null or directly modify the usr_paths variable to add the corresponding path. Both of these methods need reflection to complete.
In the figure below, the results of loadlibrary loading XBootLib.dll are printed before and after modifying the sys_paths variable
Note that System.loadLibrary loads the DLL without a suffix name. If you use System.load to load the DLL, you need to specify the full path of the dll file