Detailed explanation of cmd environment variable setting method, cmd environment variable

Source: Internet
Author: User

Detailed explanation of cmd environment variable setting method, cmd environment variable

Cmd setting environment variables can facilitate the running of the bat script, but note that the variables only apply in the current cmd window (local ), if you want to set persistent environment variables, you need to use two methods: Modify the Registry directly, and use my computer-> properties-> advanced, to set system environment variables.

1,View all currently available Environment Variables: Enter set.


2,View an environment variable: Enter"Set variable name", for example, to view the value of the temp variable, enter set temp


Of course, you can also use echo % temp %


3,Modify Environment Variables: Enter "set variable name = variable content", for example, set temp to "d: \ tempfile", as long as you enter set temp = "d: \ tempfile ". Note that this environment variable is used to overwrite the previous content, rather than append the content. For example, after setting the path above, if I re-enter set temp = "c" and check the temp path again, the value is "c ", instead of "d: \ tempfile" and "c ".

4,Set to null: To set a variable to null, enter "set variable name =. For example, "set path =" is empty when you view the path. Note: As mentioned above, it only works in the current command line window. Therefore, when viewing the path, do not right-click "my computer" -- "attribute "........

5,Append content to a variable(Unlike 3, which is overwrite): 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 path, execute "set path = % path %; c:" again. Then, when you use the set path statement to view it, there will be: d: \ tempfile; c :, instead of c :.

Environment Variables

1. view all currently available environment variables (= system variables + User variables)

set

View an environment variable, such as PATH

set PATH

Add environment variables, such as xxx = aa

set xxx=aa

Set the environment variable (such as xxx) to null.

set xxx=

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

set PATH=%PATH%;d:\xxx

[Note]: operations on environment variables in the command line mode are only valid for applications in the current window!

2. In Windows, JAVA mainly uses three environment variables: JAVA_HOME, CLASSPATH, and PATH.

1) JAVA_HOME points to the JDK installation path, for example, x: \ j2sdk1.4.2. In this path, you should be able to find the bin, lib, and other directories. Setting Method: JAVA_HOME = c: \ j2sdk1.4.2

2) role of the PATH variable
At runtime, the Java program first searches for java.exe in the region specified by the pathvariable, and takes the first value as the standard. If you do not add the setting after installing j2sdk, the C: \ WINDOWS \ system32 directory is generally used.
J2sdk1.4(some other Java development tools such as jbuilder8.pdf will copy java.exe to the C: \ WINDOWS \ system32directory after installation. Some files under this SDK need to be loaded when java.exe is executed.
For example, after j2sdk1.4 is installed, java.exe under C: \ WINDOWS \ system32will load necessary files in the C: \ Program File \ java \ directory at runtime. However, after j2sdk is installed, C: \ j2sdk1.4.2 \ bin is usually set at the beginning of the PATH variable.
When copying a volume to C: \ WINDOWS \ system32, it can overwrite java.exeof j2sdk1.4.2. at this time, the runtime java.exe will load necessary files in the directory where jbuilder8 is located.

3) functions of CLASSPATH Environment Variables
Tells the Class Loader where to find the classes and user-defined classes provided by a third party. You can also use the JVM command line parameter-classpath to specify the class path for the application, and overwrite the class path specified in-classpath to the value specified in the CLASSPATH environment variable.

3. When multiple SDK versions are installed on the machine, how can I check which SDK is used?

java -verbose

The displayed screen information shows the directory under which the system is loaded.

4. How to set PATH in Windows OS

[System]-> [environment]-> [advanced], enter C: \ j2sdk1.4.2 \ bin at the beginning of the text box of the PATH variable
You can also execute set path = c: \ j2sdk1.4.2 \ bin; % path % in the command line window. In this example, You can execute the java.exe program in any part of the command line window. Or set PATH = % JAVA_HOME % \ bin; % PATH %

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

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

The first thing to note is the front ". \;", -- backslash. This tells JDK to first find the CLASS file in the current directory when searching for the CLASS.
[Troubleshooting]

The following situations may occur during compilation to check whether you really understand the settings of environment variables and solve them.
[T1] error: Isn't java A runable program? The environment variable path is not set.
[T2] error: Cannot I open a directory? The setting sequence of directories in path environment variables may be ignored.
[T3] Exception on thread "main" java. lang. DoClassDefFoundError: Test? No classpath path is set.

Cmd view environment variables

1. view all currently available environment variables: Enter set to view them.
2. view an environment variable: Enter "set variable name". For example, to view the value of the path variable, enter set path.
3. Modify the environment variable: Enter "set variable name = variable content", for example, set path to "d: \ hacker.exe", as long as you enter set path = "d: \ nmake.exe ". Note that this environment variable is used to overwrite the previous content, rather than append the content. For example, after setting the path above, if I re-enter set path = "c" and view the path again, the value is "c :", instead of "d: \ nmake.exe" and "c ".
4. set to NULL: If you want to set a variable to null, enter "set variable name =. For example, "set path =" is empty when you view the path. Note: As mentioned above, it only works in the current command line window. Therefore, when viewing the path, do not right-click "my computer" -- "attribute "........
5. append the variable content (different from 3, which is overwrite): 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, execute "set path = % path %; c:" again. Then, when you use the set path statement to view it, there will be: d: \ hacker.exe; c :, instead of c :.

% ALLUSERSPROFILE % returns all "user configuration files.
% APPDATA % partial return the location where the application stores data by default.
% CD % returns the current directory string.
% CMDCMDLINE % returns the exact command line used to start the current Cmd.exe.
% CMDEXTVERSION % The system returns the version number of the current "command handler extension.
% COMPUTERNAME % the name of the computer returned by the system.
% COMSPEC % The system returns the exact path of the executable program of the command line interpreter.
% DATE % The 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 % The system returns the error code of the recently used command. A non-zero value is usually used to indicate an error.
% HOMEDRIVE % The system returns the local workstation drive letter that is connected to the user's home directory. Set Based on the main directory value. The user home directory is specified in "local users and groups.
% HOMEPATH % The system returns the complete path of the user's home directory. Set Based on the main directory value. The user home directory is specified in "local users and groups.
% HOMESHARE % The system returns the network path of the user's shared home directory. Set Based on the main directory value. The user home directory is specified in "local users and groups.
% LOGONSEVER % returns the name of the domain controller that verifies the current logon session.
% NUMBER_OF_PROCESSORS % Number of processors installed on the computer.
% OS % The system returns the name of the operating system. Windows 2000 displays the operating system as Windows_NT.
% PATH % The system specifies the search PATH of the executable file.
% PATHEXT % The system returns a list of file extensions that the operating system deems executable.
% PROCESSOR_ARCHITECTURE % The system returns the processor's chip architecture. Value: x86 and IA64.
% PROCESSOR_IDENTFIER % The system returns the processor description.
% PROCESSOR_LEVEL % The system returns the model of the processor installed on the computer.
% PROCESSOR_REVISION % The system returns the system variable of the processor revision number.
% PROMPT % returns the command PROMPT settings of the current interpreter. Generated by Cmd.exe.
% RANDOM % The system returns any decimal number between 0 and 32767. Generated by Cmd.exe.
% SYSTEMDRIVE % The system returns a drive containing the Windows XP root directory (that is, the system root directory.
% SYSTEMROOT % The system returns the location of the Windows XP root directory.
% TEMP % and % TMP % the system and user return the default temporary directory used by the applications available to the current login user. Some applications require TEMP, while other applications require TMP.
% TIME % The 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 % partial return the name of the domain containing the user account.
% USERNAME % returns the name of the user currently logged on.
% UserProfile % returns the location of the configuration file of the current user.
% WINDIR % the location of the operating system directory returned by the system.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.