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

Who ever 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 don’t even require any sign-in, good thing is that it can accept your lovely nick name also and URL is not mandatory too.
Thanks for your patience
~Krishna(I love "Transparency")
వీలయితే నాల్గు పోస్టులు ...కుదురితే ఒక కామెంటూ ...

Translate

Enter your email address:

Buffs ...

Visitors


hits counter
View My StatsCheck Google Page Rank

Add to Google Reader or Homepage

Bookmark and Share

Tags


Powered by WidgetsForFree