Java read operating system environment variables

xiaoxiao2021-03-06  38

/ ** File Name: Sysprob.java Description: A program that gets the current system variable. java in System.getProperty only for the JVM, if you want to get the system environment variables, but also to use the related functions of the system: Chi Qin-Email: cqq1978@Gmail.com ** /

Import java.util. *; import java.io. *;

Class sysprob {// Returns the function of the current system variable, put it in a property, here only for Win2K or more, other systems can improve public property getenv () throws exception {property pROP = new property (); string OS = System.GetProperty ("Os.Name"). TOLOWERCASE (); Process P = NULL; IF (Os.Indexof ("Windows")> - 1) {P = runtime.getRuntime (). EXEC ("CMD / C SET "); // other operating system can be handled by itself, I am win2k} bufferedreader br = new buffreader (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 VOID Main (String [] args) {try {sysprob sp = new sysprob (); Properties P = sp.Getenv (); system.out .println ("p.GetProperty (" path ")); // pay attention to case, if it is written, it is not correct} catch (exception e) {system.out.println (e);}

}

转载请注明原文地址:https://www.9cbs.com/read-82267.html

New Post(0)