Modify environment variables through command line/batch processing in XP

Source: Internet
Author: User

Reprinted from http://www.slyar.com/blog/xp-java-reg-env.html

 

 

The environment variables of the XP system are stored in the registry. to modify the environment variables through command lines or batch processing, you need to know where the environment variables are stored in the registry.

The PS. CMD command can be used to directly modify the environment variables through the set command, but it is only one-time and will be invalid after the system is restarted. This is why the registry should be modified directly.

The Registry Key of the system environment variable space is saved:

HKEY_LOCAL_MACHINE/system/CurrentControlSet/control/Session Manager/Environment

The Registry Key of the current user's environment variable space is saved:

HKEY_CURRENT_USER/Environment

With this, we can use the reg command of CMD to modify the Registry. For the reg command, You can Google it on your own. There are many usage cases.

You can write the CMD statement for modifying environment variables into batch processing. Of course, you can also use the C system ("command"); statement to write it into a small program.

Here, I write three statements to modify the JDK environment variables.

Reg Add "HKEY_LOCAL_MACHINE/system/CurrentControlSet/control/Session Manager/environment"/V java_home/T REG_SZ/D "% Cd %"/F

This statement adds the java_home environment variable. The value of the variable is the path of the batch processing. Therefore, after writing these three statements to batch processing, you only need to place the batch processing file under the JDK installation directory, you can configure the environment variables at one time.

Reg Add "HKEY_LOCAL_MACHINE/system/CurrentControlSet/control/Session Manager/environment"/V classpath/T reg_expand_sz/D ".; % java_home %/lib/DT. jar; % java_home %/lib/tools. jar; % java_home %/lib/htmlconverter. jar "/F

This statement adds the classpath environment variable.

Reg Add "HKEY_LOCAL_MACHINE/system/CurrentControlSet/control/Session Manager/environment"/V path/T reg_expand_sz/D ".; % PATH %; % java_home %/bin"/F

This statement modifies the PATH environment variable and uses % PATH % to read the previous PATH variable. Otherwise, the original PATH variable will be cleared.

With the core content, other modifications will be very casual. You can do it now, hey.

Well, let's take a look at the meaning of common system environment variables in this article.

Important changes:

Recently, when batch processing was used, it was always impossible to succeed at a time. Just now, after careful research, we found a series of very important problems!

1. The % variable is automatically converted during batch processing.

2. When you use the reg command to modify the registry, if % variable % has a valid value, % variable % will be automatically replaced; If % variable % does not exist, the original output will remain unchanged.

After many experiments, I found these two key problems. It doesn't matter if I understand them. I can understand them. If you want to successfully use batch modification, you must modify two items. The first is that % in the batch processing must use the Escape Character % to maintain the variable symbol, and the second is that it must be finally poured into java_home, so that the variable will not be automatically converted when % java_home % is called before. In short, the final effective batch processing is as follows:

@ Reg Add "HKEY_LOCAL_MACHINE/system/CurrentControlSet/control/Session Manager/environment"/V classpath/T reg_expand_sz/D ".; % java_home %/lib/DT. jar; % java_home %/lib/tools. jar; % java_home %/lib/htmlconverter. jar "/F

@ Reg Add "HKEY_LOCAL_MACHINE/system/CurrentControlSet/control/Session Manager/environment"/V path/T reg_expand_sz/D ".; % PATH %; % java_home %/bin "/F

@ Reg Add "HKEY_LOCAL_MACHINE/system/CurrentControlSet/control/Session Manager/environment"/V java_home/T REG_SZ/D "% Cd %"/F

Usage: Save the above Code as JDK. BAT and place it in the JDK installation directory !!! Double-click to complete environment variable configuration.

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.