java 不能直接修改windows系统的环境变量,需要借助JNI转为C++的接口,以调用windows系统的注册表
如其说修改环境变量,不如说修改注册表更准确些,因为是通过修改注册表来实现修改环境变量的;“环境变量”的键值所在位
置:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session
Manager\Environment
Windows Registry API Native Interface下载地址
http://www.trustice.com/java/jnireg/index.shtml 下
registry-3.1.3.zip(包含源代码)。解开 registry-3.1.3.zip,在 bin 目录中可以看到两个文件
ICE_JNIRegistry.dll 和registry.jar,动态库就是本地代码实现。
 1 package frame;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.InputStreamReader;
 5 
 6 import com.ice.jni.registry.RegStringValue;
 7 import com.ice.jni.registry.Registry;
 8 import com.ice.jni.registry.RegistryKey;
 9 
10 public class TestRegistry {
11 
12     /**
13      * @param args
14      */
15     public static void main(String[] args) {
16         // TODO Auto-generated method stub
17         try {
18             //RegistryKey openPath1 
19             //    = Registry.HKEY_LOCAL_MACHINE.openSubKey("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment");
20             //String path_Old = openPath1.getStringValue("PAODING_DIC_HOME");    //获取原Path键值    
21             //System.out.println(path_Old);
22             //System.out.println(System.getProperty("user.dir"));
23             
24             RegistryKey openPath2 
25                 = Registry.HKEY_LOCAL_MACHINE.openSubKey("SYSTEM\\CurrentControlSet\\Control\\Session Manager");
26             RegistryKey subKey = openPath2.createSubKey("Environment", "");
27             subKey.setValue(new RegStringValue(subKey, "PAODING_DIC_HOME", "D:\\PaodingEnv\\Dic")); //修改Path键值
28             subKey.closeKey();
29             /*RegistryKey openPath2 
30                 = Registry.HKEY_LOCAL_MACHINE.openSubKey("SYSTEM\\CurrentControlSet\\Control\\Session Manager");
31 
32             RegistryKey subKey = openPath2.createSubKey("Environment", "");
33             //定义Path所在目录的句柄(相当于在Session Manager路径下面,新建Environment文件夹,如果存在不改变已有的值。)
34             //   String path_New = path_Old + ";" + "D:\\myTinoProject\\bingy";
35             String path_New = path_Old + "bin;";
36             subKey.setValue(new RegStringValue(subKey, "Path", path_New)); //修改Path键值
37             subKey.closeKey();
38             //查看进程的方法
39             String[] cmd = { "D:\\dfqd\\workspace\\tasklist" };
40             Process proc = Runtime.getRuntime().exec(cmd);
41             BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
42             String string_Temp = in.readLine();
43             while (string_Temp != null) {
44                 System.out.println(string_Temp);
45                 string_Temp = in.readLine();
46             }
47             //删除explorer.exe进程
48             Process proc2 = Runtime.getRuntime().exec("D:\\dfqd\\workspace\\taskkill /F /IM explorer.exe");
49             Thread.sleep(500);
50             //重启explorer.exe进程
51             Process proc3 = Runtime.getRuntime().exec("explorer.exe");
52             System.out.println("=====SUCCESS=====");*/
53         } 
54         catch (Exception e) {
55             e.printStackTrace();    
56         }
57     }
58 }
59