Java read operating system environment variables

Source: Internet
Author: User
Tags variables
Variable Java read operating system environment variable








/**


FileName: Sysprob.java


Description: Gets the program for the current system variable. The System.getproperty in Java is for the JVM only, and the system-related functions are used if you want to obtain system environment variables


Author: Ci Qin Qiang


email:cqq1978@gmail.com


**/











import java.util.*;


import java.io.*;








class Sysprob


{


//Returns the current system variable's function, the result is placed in a properties inside, here only for Win2K above, other systems can improve themselves


public Properties GetEnv () throws Exception


{


Properties Prop=new properties ();


String OS = System.getproperty ("Os.name"). toLowerCase ();


Process P=null;


if (Os.indexof ("Windows") >-1)


{


p=runtime.getruntime (). EXEC ("cmd/c set"); Other operating systems can be handled by themselves, I'm here Win2K


}


BufferedReader br=new BufferedReader (New InputStreamReader (P.getinputstream ()));


String Line;


while ((Line=br.readline ())!=null)


{


int I=line.indexof ("=");


if (i>-1)


{


String key=line.substring (0,i);


String value=line.substring (i+1);


Prop.setproperty (Key,value);


}


}


return prop;


}





//Specific usage


public static void Main (string[] args)


{


Try


{


sysprob sp=new sysprob ();


Properties p=sp.getenv ();


System.out.println (P.getproperty ("Path")); Note that case, if you write path, it's not right.


}


catch (Exception e)


{


System.out.println (e);


}





}


}

















Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.