通过System获取java环境变量的路径代码为:
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Map; import java.util.Properties; public class Sys { public static void main(String[] args) throws Exception { //获取素所有的环境变量 Map<String,String> env = System.getenv(); for (String name : env.keySet()) { System.out.println(env.get(name)); } //获取指定环境变量的值 System.out.println(System.getenv("JAVA_HOME")); //获取所有系统属性 Properties props = System.getProperties(); //将所有的系统文件保存 props.store(new FileOutputStream("C:\Users\Administrator\Desktop\配置.txt"),"System Properties"); //输出特定的系统属性 System.out.println(System.getProperty("os.name")); } }
输出结果为:
C:UsersAdministratorAppDataLocal 6 NO MS-20160512ARFF \MS-20160512ARFF C:jdk64jdk Console C:ProgramData x86 C:Windowssystem32WindowsPowerShellv1.0Modules C: Destination=file C:UsersAdministratorAppDataRoaming Administrator C:BVTBinTestsinstallpackagecsilogfile.log C:Program Files (x86) C:Program Files (x86)Common Files C:/Program Files (x86)/Java/jre1.8.0_144/bin/client;C:/Program Files (x86)/Java/jre1.8.0_144/bin;C:/Program Files (x86)/Java/jre1.8.0_144/lib/i386;C:ProgramDataOracleJavajavapath;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;C:jdk64jdkin;C:jdk64jdkjrein;D:JavaIdein;C:Program Files (x86)CanonEasy-WebPrint EXin;D:JavaIdemysqlinC:jdk64jdkin;C:jdk64jdkjrein;;C:UsersAdministratoreclipsejava-oxygeneclipse; .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC Windows_NT AMD64 3 MS-20160512ARFF 1 3a09 .;C:jdk64jdklib;C:jdk64jdklib ools.jar C:Program FilesCommon Files C:Windowssystem32cmd.exe C:ProgramData C:Program Files UsersAdministrator C:Windows C:UsersADMINI~1AppDataLocalTemp C: Intel64 Family 6 Model 58 Stepping 9, GenuineIntel C:UsersAdministrator C:UsersADMINI~1AppDataLocalTemp C:Program Files (x86)Common Files C:Program Files (x86) C:UsersPublic 4 C:Windows C:jdk64jdk Windows 7