Even though most of the languages does not allow (or) discourage to change the environment variables through program, you can achieve that with JNI in java using setenv()
and using ProcessBuilder()
.
But why do you want to change something for every one from your program? Instead change the variables in your program context like setting the proxy server so that it could be effective only for your program run time context. That's how the applications should be designed and programmed.
Here is an example, off the top of head.
public static void main(String[] args) throws Exception
{
ProcessBuilder processBuilder = new ProcessBuilder("CMD.exe", "/C", "SET");
processBuilder.redirectErrorStream(true);
Map<String,String> environment = processBuilder.environment();
//Set the new envrionment varialbes here
environment.put("proxySet", "true");
environment.put("http.proxyHost", proxyUrl);
environment.put("http.proxyPort", proxyPort);
Process process = processBuilder.start();
BufferedReader inputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String dataLog=null;
while ((dataLog = inputReader.readLine()) != null)
{
//Just to see what's going on with process
System.out.println(dataLog);
}
}
Note: Again, discourage the practice of changing environment variables from your program, instead set the required ones for just your context.