How to set / get windows environment variables through Java code?

How to set / get windows environment variables through Java code?

Below simple program is self explained on how to use set / get the windows environment variables.

    import java.io.*;  
    import java.util.*;  
      
    public class Test  
    {  
        public static void main(String[] args) throws Exception  
        {  
            ProcessBuilder pb = new ProcessBuilder("CMD.exe", "/C", "SET"); // SET prints out the environment variables  
            pb.redirectErrorStream(true);  
            Map env = pb.environment();  
            String path = env.get("Path") + ";C:\\cygwin\\bin";  
            env.put("Path", path);  
            Process process = pb.start();  
            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));  
            String line;  
            while ((line = in.readLine()) != null)  
            {  
                System.out.println(line);  
            }  
        }  
    }  


0 comments to "How to set / get windows environment variables through Java code?"

Post a Comment

Whoever writes Inappropriate/Vulgar comments to context, generally want to be anonymous …So I hope U r not the one like that?
For lazy logs, u can at least use Name/URL option which doesn’t even require any sign-in, The good thing is that it can accept your lovely nick name also and the URL is not mandatory too.
Thanks for your patience
~Krishna(I love "Transparency")

Popular Posts

Enter your email address:

Buffs ...

Tags


Powered by WidgetsForFree