It is divided into some environment variables that get Java itself and the environment variables related to the operating system.
- Get some of the JVM-related variables
Set an environment variable at run time debug to True:java-ddebug=true YourClass
Set an environment variable in the program debug to True: system.setproperty ("Debug", "true");
Get an environment variable debug: String debug = System.getproperty ("Debug");
Some of the environment variables are listed in the following table, which are already defined by Java and can be obtained through System.getproperty ("key") in the program, and more environment variables see the JDK documentation.
Property name |
Description |
Java.version |
Java Runtime version |
Java.home |
installation directory for Java |
Java.class.version |
Version number of the Java class format |
Java.class.path |
Find paths for Java classes |
Java.io.tmpdir |
The default temp directory |
Java.compiler |
A timely compiler used by Java |
Java.ext.dirs |
Directory for Java expansion packs |
Os.name |
Name of the operating system |
Os.arch |
Architecture of the operating system |
Os.version |
Version of the operating system |
File.separator |
File delimiter ("/" under Unix) |
Path.separator |
Path delimiter (under Unix as ': ') |
Line.separator |
Line break ('/n ' under Unix) |
User.Name |
User account Name |
User.home |
User Directory |
User.dir |
User's current working directory |
Other specific references can be found in the following code instance program.
- Get environment variables for operating system
Note that this is the environment variable that gets the operating system, not some variables related to the JVM.
Perhaps in order to create a JVM is the operating system platform atmosphere, or to emphasize the platform-independent Java, not know Java has put system.getenv (String) function obsolete. So in general Java can only get some of its own defined variables, but not with the operating system environment variables, can only run by Java's "-D" parameter to set some of the variables to be passed to it.
So the only way is to judge the operating system first, then use the operating system commands to bring up the list of environment variables, try to obtain the output list.
Import Java.io.bufferedreader;import java.io.ioexception;import Java.io.inputstreamreader;import Java.util.HashMap Import Java.util.map;import Java.util.properties;public class Systemvalue {/** * get system-related values by using systems */public static Voi D getsystemproperties () {Properties pp = system.getproperties (); System.out.println ("System ' s Properties:"); System.out.println (); Java.util.Enumeration en = Pp.propertynames (); while (En.hasmoreelements ()) {String Nexte = (string) en.nextelement (); System.out.print (nexte + "=" + Pp.getproperty (nexte)); System.out.println (); }} public static void Getcustomproperties (String key) {map map = GETENV (); SYSTEM.OUT.PRINTLN (key + "=" + map.get (key)); public static map GetEnv () {map map = new HashMap (); Process p = null; Runtime r = Runtime.getruntime (); String OS = System.getproperty ("Os.name"). toLowerCase (); System.out.println ("os=" +os); try {if (Os.indexof ("Windows 9") >-1) {p = r.exec ("command.com/c set"); } else if ((Os.indexof ("NT") >-1) | | (Os.indexof ("Windows 1") >-) | | (Os.indexof ("Windows XP") >-1)) {p = r.exec ("cmd.exe/c set"); } else {//Unix P = r.exec ("env"); } bufferedreader br = new BufferedReader (new InputStreamReader (p. getInputStream ())); String Line; while (line = Br.readline ()) = null) {string[] str = line.split ("="); Map.put (Str[0], str[1]); }} catch (IOException IoE) {ioe.printstacktrace (); } return map; } public static void Main (string[] args) {//getsystemproperties (); Getcustomproperties ("Java_home"); }}
Getting environment variables in Java
Getting environment variables in Java