开发者

Adding environment variables using java program and able to see when I open environment tab from My Computer

开发者 https://www.devze.com 2023-04-09 13:32 出处:网络
I want to write java program which adds environment variables and when I open environment tab from MyComputer,then I should be able to see that...

I want to write java program which adds environment variables and when I open environment tab from MyComputer,then I should be able to see that...

This is what I have tried,but it gives java.lang.unsupportedexception

Map env = System.getenv(); env.put("abc", "pqr");

And One more try is below,it is not giving any error...But I can not see any value added when I open environment variables tablenter code here from My Computer.But When i sysout "env" variable it gives me all paths including myone also...But I need same thing to be shwon in environment variables tab...

ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe","/c","set");      
Map env 开发者_C百科= processBuilder.environment();
env.put("abc", "pqr");  

Please help me guys...Thanks in Advance...


There are two ways. The first one is to call the Windows API to change/set the environment variable. You must look for the right Windows API function and call it from Java. However calling any Windows API from Java is a topic on its own.

The second way is to call the setx.exe program with the correct arguments to set environment variables. Check the manual of the setx.exe program how to use it (and when it is installed) to set your environment variables.

In both ways you obviously restrict your Java program to run on Windows systems only.

0

精彩评论

暂无评论...
验证码 换一张
取 消