Java read operating system environment variables

xiaoxiao2021-03-06  37

Java read operating system environment variable / ** file name: sysprob.java Description: A program that acquires 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, other systems can improve public property getenv () throws exception {property pROP = New Properties (); String OS = System.getProperty ("Os.Name"). TOLOWERCASE (); Process P = NULL; IF (Os.Indexof ("Windows")> - 1) {P = runtime.getRuntime (). EXEC. EXEC. EXEC ("cmd / c set"); // other operating system can handle itself, I am Win2k} bufferedreader br = new buffreader (p.GETInputStream ())); string line; while ((line = br) .readLine ())! = null) {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")); // pay attention to case, if it is written, it is wrong} catch (Exception E) {system.out.println (e);}}}

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

New Post(0)