AurelianR Posted April 30, 2008 Report Share Posted April 30, 2008 Hello!I want to set the environment variables for an user from cmd !I found a command which set global system environment variable : "cmd /c reg add \"HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\" /v \"{0}\" /d \"{1}\" /t REG_EXPAND_SZ"Waiting your feedback!Best regards, Quote Link to comment Share on other sites More sharing options...
ɹəuəllıʍ ʇɐb Posted April 30, 2008 Report Share Posted April 30, 2008 Welcome to the Windows Forum.Very interesting; thank you! Did you actually test it, and does it work? Quote Link to comment Share on other sites More sharing options...
AurelianR Posted April 30, 2008 Author Report Share Posted April 30, 2008 Hello!I want to set the environment variables for an user from cmd !I found a command which set global system environment variable : "cmd /c reg add \"HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\" /v \"{0}\" /d \"{1}\" /t REG_EXPAND_SZ"Waiting your feedback!Best regards,Ithe command which sets the user environment variable is:"cmd /c reg add \"HKEY_CURRENT_USER\\Environment\" /v \"{0}\" /d \"{1}\" /t REG_EXPAND_SZ"; Quote Link to comment Share on other sites More sharing options...
AurelianR Posted April 30, 2008 Author Report Share Posted April 30, 2008 Welcome to the Windows Forum.Very interesting; thank you! Did you actually test it, and does it work?Yes it is work I used both in a java program here is the code :package engine;import java.text.MessageFormat;public class Environment{ static final String REG_ADD_CMD = "cmd /c reg add \"HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\" /v \"{0}\" /d \"{1}\" /t REG_EXPAND_SZ"; static final String REG_ADD_USER_CMD = "cmd /c reg add \"HKEY_CURRENT_USER\\Environment\" /v \"{0}\" /d \"{1}\" /t REG_EXPAND_SZ"; static String environmentVaiable = new String(); public Environment(){ } void exec(String key, String value) throws Exception {// if (args.length != 2)// throw new IllegalArgumentException("\n\nUsage: java SetEnv {key} {value}\n\n"); key = "testKeyU"; value = "testValueU"; String cmdLine = MessageFormat.format(REG_ADD_CMD, new Object[] { key, value }); Runtime.getRuntime().exec(cmdLine); } public void userExec(String key, String value) throws Exception {// if (args.length != 2)// throw new IllegalArgumentException("\n\nUsage: java SetEnv {key} {value}\n\n"); key = "testKey"; value = "testValue"; String cmdLine = MessageFormat.format(REG_ADD_USER_CMD, new Object[] { key, value }); Runtime.getRuntime().exec(cmdLine); } public static String getEnv(String env){ environmentVaiable = System.getenv(env); return environmentVaiable; } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.