Home  >  Article  >  Backend Development  >  cmd environment variable setting method example sharing

cmd environment variable setting method example sharing

小云云
小云云Original
2018-02-02 11:34:495226browse

Setting environment variables in cmd can facilitate the running of our bat script, but it should be noted that the variables only have an effect in the current cmd window (local effect). If we want to set persistent environment variables, we need to set them by two means. : One is to modify the registry directly, and the other is to set the system environment variables through My Computer->Properties->Advanced.

1, View all currently available environment variables: Enter set to view.


2. Check an environment variable: Enter "set variable name". For example, if you want to check the value of the temp variable, that is Enter set temp


. Of course, you can also use echo %temp%


##3,

Modify environment variables : Enter "set variable name = variable content". For example, to set temp to "d:\tempfile", just enter set temp="d:\tempfile". Note that this modification of the environment variable refers to overwriting the previous content with the current content, not appending. For example, after I set the path above, if I re-enter set temp="c", when I check the temp path again, the value is "c", not "d:\tempfile"; "c".

4,

Set to empty: If you want to set a certain variable to empty, enter "set variable name=". For example, "set path=" then the path will be empty when you check it. Note that, as mentioned above, it only works in the current command line window. Therefore, when viewing the path, do not right-click "My Computer" - "Properties"...

5,

Append content to the variable (different from 3, That is overwriting): Enter "set variable name=% variable name%; variable content". For example, to add a new path for temp, enter "set path=%path%;d:\tempfile" to add d:\tempfile to the path, and execute "set path=%path%;c:" again. Then, when you use the set path statement to view, there will be: d:\tempfile;c:, instead of only c: like in step 3.

Detailed explanation of environment variables

1. View all currently available environment variables (=system variables + user variables)

set

View an environment variable, such as PATH

set PATH

Add an environment variable, such as xxx=aa

set xxx=aa

Set the value of the environment variable (such as xxx) to empty

set xxx=

Add a new value (such as d:\xxx) after an environment variable (such as PATH)

set PATH=%PATH%;d:\xxx

[Note]: The operation of environment variables through the command line is only valid for the application in the current window!

2. There are three main environment variables used by JAVA under Windows: JAVA_HOME, CLASSPATH, and PATH.

1) JAVA_HOME points to the JDK installation path, such as x:\j2sdk1.4.2. You should be able to find bin, lib and other directories under this path. Setting method: JAVA_HOME=c:\ j2sdk1.4.2

2) The role of the PATH variable

When the java program is running, it first looks for java.exe in the path defined by the path variable, and finds it first shall prevail. If no settings are added after installing j2sdk, it is usually the C:\WINDOWS\system32 directory.
j2sdk1.4 (and other java development tools such as jbuilder8) will copy java.exe to the C:\WINDOWS\system32 directory after installation. When executing java.exe, some files under this SDK need to be loaded. .
For example, after the installation of j2sdk1.4 is completed, java.exe under C:\WINDOWS\system32 will load some necessary files in the C:\Program File\java\ directory when running. However, after installing j2sdk, C:\j2sdk1.4.2\bin will generally be set at the front of the PATH variable.
When installing j2sdk1.4.2 first, and then installing development tools such as jbuilder8, because the java.exe of jbuilder8 may overwrite the java.exe of j2sdk1.4.2 when copied to C:\WINDOWS\system32, then the java.exe running at this time java.exe will go to the directory where jbuilder8 is located to load some necessary files.

3) The role of the CLASSPATH environment variable

tells the class loader where to find classes provided by third parties and user-defined classes. You can also use the JVM command line parameter -classpath to specify a class path for the application respectively. The class path specified in -classpath overrides the value specified in the CLASSPATH environment variable.

3. When there are multiple SDK versions installed in the machine, how to check which SDK is used?

java -verbose

You can see in the screen information that appears which directory the file is loaded by the system.


4. How to set PATH under Windows OS

[System]->[Environment]-> [Advanced], in the PATH variable Enter C:\j2sdk1.4.2\bin

at the front of the text box or execute set path=c:\j2sdk1.4.2\bin;%path%; in the command line window so that any path in the command line window The java.exe program can be executed at any time. Or set PATH=%JAVA_HOME%\bin;%PATH%

5. Be extra careful when setting the CLASSPATH environment variable, because more than 80% of the inexplicable strange problems you may encounter in the future may be caused by incorrect CLASSPATH settings.

CLASSPATH=.\;%JAVA_HOME%\lib\tools.jar

The first thing to pay attention to is the first ".\;", - period backslash points Number. This tells the JDK to first search for the CLASS file in the current directory when searching for CLASS.
【Troubleshooting】

The following situations will occur during compilation. See if you really understand the settings of environment variables and can solve them.
[T1]error: java is not an operable program? Because the environment variable path
[T2] error: cannot open a directory? It may be that the setting order of the directories in the path environment variable has been ignored.
[T3]Exception on thread “main” java.lang.DoClassDefFoundError:Test? The path to classpath is not set.

cmd View environment variables

1. View all currently available environment variables: Enter set to view.
2. To view an environment variable: enter "set variable name". For example, if you want to view the value of the path variable, enter set path
3. To modify the environment variable: enter "set variable name = variable content" That's it. For example, to set path to "d:\hacker.exe", just enter set path="d:\nmake.exe". Note that this modification of the environment variable refers to overwriting the previous content with the current content, not appending. For example, after I set the path above, if I re-enter set path="c", when I check the path again, the value is "c:", not "d:\nmake.exe";" c".
4. Set to empty: If you want to set a certain variable to empty, enter "set variable name=". For example, "set path=" then the path will be empty when you check it. Note that, as mentioned above, it only works in the current command line window. Therefore, when viewing the path, do not right-click "My Computer" - "Properties"...
5. Append content to the variable (different from 3, which is overwriting): Enter "set variable Name=%Variable name%;Variable content”. For example, to add a new path to path, enter "set path=%path%;d:\hacker.exe" to add d:\hacker.exe to path, and execute "set path=%path%; again" c:", then, when using the set path statement to view, there will be: d:\hacker.exe;c:, instead of only c: like in step 3.

%ALLUSERSPROFILE% Local Returns the location of all User Profiles.
%APPDATA% Local Returns the location where the application stores data by default.
%CD% Local Returns the current directory string.
%CMDCMDLINE% Local Returns the exact command line used to start the current Cmd.exe.
%CMDEXTVERSION% System Returns the current version number of the "Command Handler Extension".
%COMPUTERNAME% System Returns the name of the computer.
%COMSPEC% System Returns the exact path to the command line interpreter executable program.
%DATE% System Returns the current date. Use the same format as the date /t command. Generated by Cmd.exe. For more information about the date command, see Date.
%ERRORLEVEL% System Returns the error code for the most recently used command. Usually a non-zero value indicates an error.
%HOMEDRIVE% System Returns the local workstation drive letter connected to the user's home directory. Setting based on home directory value. The user home directory is specified in Local Users and Groups.
%HOMEPATH% System Returns the full path to the user's home directory. Setting based on home directory value. The user home directory is specified in Local Users and Groups.
%HOMESHARE% System Returns the network path to the user's shared home directory. Setting based on home directory value. The user home directory is specified in Local Users and Groups.
%LOGONSEVER% Local Returns the name of the domain controller authenticating the current login session.
%NUMBER_OF_PROCESSORS% System Specifies the number of processors installed on the computer.
%OS% System Returns the name of the operating system. Windows 2000 displays the operating system as Windows_NT.
%PATH% System Specifies the search path for executable files.
%PATHEXT% System Returns a list of file extensions that the operating system considers executable.
%PROCESSOR_ARCHITECTURE% System Returns the chip architecture of the processor. Values: x86, IA64.
%PROCESSOR_IDENTFIER% System Returns the processor description.
%PROCESSOR_LEVEL% System Returns the model number of the processor installed on the computer.
%PROCESSOR_REVISION% System System variable that returns the processor revision number.
%PROMPT% Local Returns the command prompt settings for the current interpreter. Generated by Cmd.exe.
%RANDOM% System Returns any decimal number between 0 and 32767. Generated by Cmd.exe.
%SYSTEMDRIVE% System Returns the drive containing the Windows XP root directory (that is, the system root directory).
%SYSTEMROOT% System Returns the location of the Windows XP root directory.
%TEMP% and %TMP% System and User Returns the default temporary directory used by applications available to the currently logged in user. Some applications require TEMP, while others require TMP.
%TIME% System Returns the current time. Use the same format as the time /t command. Generated by Cmd.exe. For more information about the time command, see Time.
%USERDOMAIN% Local Returns the name of the domain that contains the user account.
%USERNAME% Local Returns the name of the currently logged in user.
%UserProfile% Local Returns the location of the current user's profile.
%WINDIR% System Returns the location of the operating system directory.

Related recommendations:

The environment variable process.env in Node.js will be explained in detail

The steps on how to configure environment variables under Linux Share (picture)

What are environment variables? The role of environment variables

The above is the detailed content of cmd environment variable setting method example sharing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn