通过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 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:\Users\Administrator\AppData\Local
6
NO
MS-20160512ARFF
\\MS-20160512ARFF
C:\jdk64\jdk
Console
C:\ProgramData
x86
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
C:
Destination=file
C:\Users\Administrator\AppData\Roaming
Administrator
C:\BVTBin\Tests\installpackage\csilogfile.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:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\jdk64\jdk\bin;C:\jdk64\jdk\jre\bin;D:\JavaIde\bin;C:\Program Files (x86)\Canon\Easy-WebPrint EX\bin;D:\JavaIde\mysql\binC:\jdk64\jdk\bin;C:\jdk64\jdk\jre\bin;;C:\Users\Administrator\eclipse\java-oxygen\eclipse;
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
Windows_NT
AMD64
3
MS-20160512ARFF
1
3a09
.;C:\jdk64\jdk\lib;C:\jdk64\jdk\lib\tools.jar
C:\Program Files\Common Files
C:\Windows\system32\cmd.exe
C:\ProgramData
C:\Program Files
\Users\Administrator
C:\Windows
C:\Users\ADMINI~1\AppData\Local\Temp
C:
Intel64 Family 6 Model 58 Stepping 9, GenuineIntel
C:\Users\Administrator
C:\Users\ADMINI~1\AppData\Local\Temp
C:\Program Files (x86)\Common Files
C:\Program Files (x86)
C:\Users\Public
4
C:\Windows
C:\jdk64\jdk
Windows 7