Import java. Io. bufferedreader;
Import java. Io. inputstreamreader;
Import java. util. properties;
Class Test
{
// Return the function of the current system variable. The result is placed in a properties file. Here, the function is only applicable to Win2k and later versions. Other systems can improve the function by themselves.
Public properties getenv () throws exception
{
Properties prop = new properties ();
String OS = system. getproperty ("OS. Name"). tolowercase ();
System. Out. println ("Operating System:" + OS );
PROCESS p = NULL;
If (OS. indexof ("Windows")>-1)
{
P = runtime.getruntime(cmd.exe C ("CMD/C set ");
}
If (OS. indexof ("Linux")>-1)
{
P = runtime.getruntime(cmd.exe C ("Sh-C set ");
}
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;
}
// Usage
Public static void main (string [] ARGs)
{
Try
{
Test sp = new test ();
Properties P = sp. getenv ();
System. Out. println (P. getproperty ("path"); // pay attention to Case sensitivity, such as reading path. In Linux, path is used; in windows, path is used.
}
Catch (exception E)
{
System. Out. println (E );
}
}
}