首先,使用该getenv()
方法获取环境变量-
System.out.println("PATH = " + System.getenv("PATH"));
现在,获取关键和值。循环获取环境变量列表-
Map e = System.getenv(); for (Iterator i = e.entrySet().iterator(); i.hasNext();) { Map.Entry mapEntry = (Map.Entry) i.next(); System.out.println(mapEntry.getKey() + " = " + mapEntry.getValue()); }
以下是使用Map Collection检索环境变量的示例-
import java.util.Iterator; import java.util.Map; public class Demo { public static void main(String args[]) { System.out.println("PATH = " + System.getenv("PATH")); //环境变量 Map e = System.getenv(); for (Iterator i = e.entrySet().iterator(); i.hasNext();) { Map.Entry mapEntry = (Map.Entry) i.next(); System.out.println(mapEntry.getKey() + " = " + mapEntry.getValue()); } } }
输出结果
Note: The output varies system to system. PATH = /home/cg/root/GNUstep/Tools:/usr/GNUstep/Local/Tools:/usr/GNUstep/System/Tools:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/webmaster/.local/bin:/home/webmaster/bin:/usr/local/scriba/bin:/usr/local/smlnj/bin:/usr/local/bin/std:/usr/local/bin/extra:/usr/local/fantom/bin:/usr/local/dart/bin:/usr/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin:/opt/mono/bin:/opt/mono/lib/mono/4.5:/usr/local/bin:.:/usr/libexec/sdcc:/usr/local/icon-v950/bin:/usr/local/mozart/bin:/opt/Pawn/bin:/opt/jdk1.7.0_75/bin:/opt/jdk1.7.0_75/jre/bin:/opt/pash/Source/PashConsole/bin/Debug/::/usr/local/sml/bin:/usr/local/flex/bin:/usr/local/gcc-4.8.2/bin:/usr/share/java:.:/usr/share/java:/usr/lib/jvm/java/lib:/usr/lib/jvm/java/jre/lib:/usr/local/bin:/usr/local/mozart/bin:/usr/local/go/bin:/usr/local/factor/:/usr/local/groovy-2.4.0-beta-3/bin:/opt/Pawn/bin/:/usr/local/icon-v950/bin:/usr/local/lib/mono/4.0:/usr/lib64/qtSetEnv C.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/Pawn/bin:/usr/local/scala-2.11.5/bin:/usr/libexec/gcc/x86_64-redhat-linux/4.8.2:/home/cg/root/GNUstep/Tools:/usr/GNUstep/Local/Tools:/usr/GNUstep/System/Tools:/usr/local/bin:/usr/libexec/gcc/x86_64-redhat-linux/4.9.2 PATH = /home/cg/root/GNUstep/Tools:/usr/GNUstep/Local/Tools:/usr/GNUstep/System/Tools:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/webmaster/.local/bin:/home/webmaster/bin:/usr/local/scriba/bin:/usr/local/smlnj/bin:/usr/local/bin/std:/usr/local/bin/extra:/usr/local/fantom/bin:/usr/local/dart/bin:/usr/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin:/opt/mono/bin:/opt/mono/lib/mono/4.5:/usr/local/bin:.:/usr/libexec/sdcc:/usr/local/icon-v950/bin:/usr/local/mozart/bin:/opt/Pawn/bin:/opt/jdk1.7.0_75/bin:/opt/jdk1.7.0_75/jre/bin:/opt/pash/Source/PashConsole/bin/Debug/::/usr/local/sml/bin:/usr/local/flex/bin:/usr/local/gcc-4.8.2/bin:/usr/share/java:.:/usr/share/java:/usr/lib/jvm/java/lib:/usr/lib/jvm/java/jre/lib:/usr/local/bin:/usr/local/mozart/bin:/usr/local/go/bin:/usr/local/factor/:/usr/local/groovy-2.4.0-beta-3/bin:/opt/Pawn/bin/:/usr/local/icon-v950/bin:/usr/local/lib/mono/4.0:/usr/lib64/qtSetEnv C.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/Pawn/bin:/usr/local/scala-2.11.5/bin:/usr/libexec/gcc/x86_64-redhat-linux/4.8.2:/home/cg/root/GNUstep/Tools:/usr/GNUstep/Local/Tools:/usr/GNUstep/System/Tools:/usr/local/bin:/usr/libexec/gcc/x86_64-redhat-linux/4.9.2 KOTLIN_HOME = /root/.sdkman/candidates/kotlin/current TERM = xterm SMLROOT = /usr/local/smlnj/bin XFILESEARCHPATH = /usr/dt/app-defaults/%L/Dt LIBRARY_COMBO = gnu-gnu-gnu LANG = en_US.UTF-8 GNUSTEP_NETWORK_ROOT = /usr/GNUstep/Network GNUSTEP_SYSTEM_ROOT = /usr/GNUstep/System LD_LIBRARY_PATH = /home/cg/root/GNUstep/Library/Libraries:/usr/GNUstep/Local/Library/Libraries:/usr/GNUstep/System/Library/Libraries:/usr/local/lib:/usr/lib FBR = f26 GUILE_LOAD_PATH = /home/cg/root/GNUstep/Library/Libraries/Guile:/usr/GNUstep/Local/Library/Libraries/Guile:/usr/GNUstep/System/Library/Libraries/Guile GNUSTEP_IS_FLATTENED = yes GNUSTEP_FLATTENED = yes PWD = /home/cg/root/8005768 GNUSTEP_MAKEFILES = /usr/GNUstep/System/Library/Makefiles INFOPATH = /usr/GNUstep/System/Library/Documentation/info::/usr/GNUstep/Local/Library/Documentation/info::/home/cg/root/GNUstep/Library/Documentation/info: HAXE_LIBRARY_PATH = /usr/share/haxe/std:. _ = /usr/bin/timeout SDKMAN_CANDIDATES_DIR = /root/.sdkman/candidates GNUSTEP_HOST_VENDOR = unknown HAXE_STD_PATH = /usr/local/bin/std:/usr/local/bin/extra FGC = f26 DART_SDK = /usr/local/dart DISTTAG = f26container OLDPWD = / SDKMAN_PLATFORM = Linux64