CMD environment variable setting method instance sharing

Source: Internet
Author: User
CMD setting environment variable can be convenient for our bat script to run, but note that the variable is only active in the current CMD window (local), if you want to set a persistent environment variable we need to set up by two means: one is to modify the registry directly, the other is through my computer-the property-〉 advanced To set the environment variables for the system.

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


2. View an environment variable: Enter "Set variable name", for example to see the value of the TEMP variable, that is, enter set TEMP


Of course, you can also use echo%temp%


3, Modify the environment variable : Enter "Set Variable name = variable content", such as set temp to "D:\tempfile", as long as the input set temp= "D:\tempfile". Note that this modified environment variable refers to the current content to overwrite the previous content, not append. For example, when I set the path above, if I re-enter set temp= "C" again, when I look at the temp path again, the value is "C" instead of "d:\tempfile"; "C".

4. set to NULL : If you want to set a variable to NULL, enter "Set Variable name =". If "Set path=" then look at the path is empty. Note that the above has been said to work only in the current command-line window. So don't right-click on "My Computer"-"properties" when viewing path.

5. append content to variable (different from 3, that is overwrite): enter "Set variable name =% variable name%; variable Contents". For example, to add a new path for temp, enter "set path=%path%;d: \tempfile" to add d:\tempfile to path and execute "set Path=%path%;c:" Again, then use the set Path statement to view, there will be: d:\tempfile;c: Instead of just C: in the 3rd step.

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 environment variables, such as Xxx=aa

set xxx=aa

Set the value of an 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]: The operation of the environment variable in the command line is only valid for the application of the current window!

2, there are 3 main environment variables used in JAVA under Windows: Java_home, CLASSPATH, 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
Java program in the run time first in the path variable defined paths to find Java.exe, and the first to find the prevail, if installed after J2SDK is not set, is generally C:\WINDOWS\system32 directory.
j2sdk1.4 (as well as other Java development tools such as Jbuilder8) will copy the Java.exe to the C:\WINDOWS\system32 directory after installation, and some of the files under this SDK will need to be loaded when the Java.exe is executed.
If j2sdk1.4 is installed, the Java.exe under C:\WINDOWS\system32 will load the required files in the C:\Program file\java\ directory at run time. However, after installing J2SDK, C: \ j2sdk1.4.2 \ bin is usually set at the front of the path variable.
Yourselves install j2sdk1.4.2, after installing jbuilder8 and other development tools, because Jbuilder8 java.exe in copy to C:\WINDOWS\ System32 may overwrite j2sdk1.4.2 's java.exe, then the running Java.exe will go to the directory where Jbuilder8 is located to load some required files.

3) Role of CLASSPATH environment variables
Tell the class loader where to look for classes provided by third parties and user-defined classes. You can also use the JVM command-line parameter-classpath to specify the classpath for the application, and the classpath specified in-classpath overrides the value specified in the CLASSPATH environment variable.

3. How do I see which SDK is used when there are multiple SDK versions installed in the machine?

java -verbose

In the screen information that appears, you can see which directory the system is loading files from.

4. How to set path under Windows OS

(System)->(Environment)->(Advanced), enter C + + J2sdk1.4.2\bin in the text box of the PATH variable
or execute set path=c:\j2sdk1.4.2\bin;%path% in the Command line window; This allows the Java.exe program to be executed under either path in the command-line window. or set path=%java_home%\bin;%path%

5, for the CLASSPATH environment variable setting method to be more careful, because later you appear inexplicable 80% above the strange problem may be due to the classpath set wrong.

Classpath=.\;%java_home%\lib\tools.jar

The first thing to note is the front ". \;", the--period backslash semicolon. This is the class file that tells the JDK to find the current directory when searching for class.
"Troubleshooting"

The following situation occurs in the compilation to see if you really understand the settings of the environment variable and can resolve it.
[T1]error:java is not a program that can be run? Because the environment variable path is not set
[T2] Error: Cannot open a directory? It is possible to ignore the order in which directories in the PATH environment variable are set.
[T3] Exception on thread "main" Java.lang.DoClassDefFoundError:Test? The path to the classpath is not set.

CMD View environment variables

1. View all currently available environment variables: Enter set to view.
2. View an environment variable: enter "Set Variable name", for example, to see the value of the path variable, that is, enter the set path
3, modify the environment variable: enter "Set Variable name = variable content", such as set path to "D:\hacker.exe", as long as the input set path= "D:\nmake.exe". Note that this modified environment variable refers to the current content to overwrite the previous content, not append. For example, when I set the path above, if I re-enter set path= "C" again, when I look at the path, the value is "C:" instead of "D:\nmake.exe"; C ".
4. Set to NULL: If you want to set a variable to NULL, enter "Set Variable name =". If "Set path=" then look at the path is empty. Note that the above has been said to work only in the current command-line window. So don't right-click on "My Computer"-"properties" when viewing path.
5. Append content to variable (different from 3, that is overwrite): enter "Set variable name =% variable name%; variable Contents". For example, to add a new path for path, enter "Set path=%path%;d: \hacker.exe" to add D:\hacker.exe to path and execute "set Path=%path%;c:" Again, then use the set Path statement to view, there will be: d:\hacker.exe;c: Instead of just C: in the 3rd step.

%allusersprofile% returns the location of all user profiles locally.
%appdata% Local returns the location by default where the application stores data.
%cd% returns the current directory string locally.
%cmdcmdline% Local returns the exact command line used to start the current Cmd.exe.
The%cmdextversion% system returns the version number of the current command handler extension.
The%computername% system returns the name of the computer.
The%comspec% system returns the exact path to the command-line interpreter executable program.
The%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.
The%errorlevel% system returns the error code for the most recently used command. Errors are usually indicated by a value other than 0.
The%homedrive% system returns the local workstation drive letter that is connected to the user's home directory. Settings based on the home directory value. The user home directory is specified in Local Users and groups.
The%homepath% system returns the full path of the user's home directory. Settings based on the home directory value. The user home directory is specified in Local Users and groups.
The%homeshare% system returns the network path of the user's shared home directory. Settings based on the home directory value. The user home directory is specified in Local Users and groups.
%logonsever% Local returns the name of the domain controller that validates the current logon session.
The%number_of_processors% system specifies the number of processors installed on the computer.
The%os% system returns the name of the operating system. Windows 2000 displays the operating system as WINDOWS_NT.
The%PATH% system specifies the search path for the executable file.
The%pathext% system returns a list of file name extensions that the operating system considers executable.
The%processor_architecture% system returns the chipset architecture of the processor. Value: X86,ia64.
The%processor_identfier% system returns the processor description.
The%processor_level% system returns the model number of the processor installed on the computer.
The system variable that returns the processor revision number for the%processor_revision% system.
%prompt% returns the command prompt settings for the current interpreter locally. Generated by Cmd.exe.
The%random% system returns any decimal digits from 0 to 32767. Generated by Cmd.exe.
The%systemdrive% system returns a drive that contains the Windows XP root directory, which is the system root.
The%SYSTEMROOT% system returns the location of the Windows XP root directory.
The%temp% and%TMP% system and the user return the default temporary directory used by the application that is available to the currently logged-on user. Some applications require TEMP, while other applications require TMP.
The%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-on user.
%UserProfile% Local Returns the location of the current user's profile.
The%WINDIR% system returns the location of the operating system directory.

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.