可以通过System类的getProperty()方法来获取环境变量的值。例如,要获取名为"PATH"的环境变量的值,可以使用以下代码:
String pathValue = https://www.yisu.com/ask/System.getProperty("PATH"); System.out.println("PATH环境变量的值是:" + pathValue);
另外,可以使用System类的getenv()方法获取所有的环境变量,返回的是一个Map类型的数据结构,其中key是环境变量的名称,value是对应的值。例如:
MapenvVariables = System.getenv(); for (Map.Entry entry : envVariables.entrySet()) { System.out.println(entry.getKey() + " : " + entry.getValue()); }