import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
public class ReadEnv {
public static void main(String[] args) {
//getEnv();
//getProperties();
getRuntime();
}
/**
* java调用windows系统命令
* */
public static void getRuntime(){
Runtime rt = Runtime.getRuntime();
byte[] env = new byte[1000];
try {
Process process = rt.exec("cmd /c echo %classpath%");//cmd /c echo %classpath%
InputStream is = process.getInputStream();
is.read(env);
System.out.println(new String(env).trim());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 根据System.getenv()获取
* */
public static void getEnv(){
Map<String,String> envMap = System.getenv();
Iterator<String> envitr = envMap.keySet().iterator();
while(envitr.hasNext()){
System.out.println(envMap.get(envitr.next()));
}
}
/**
* 根据System.getProperties()获取
* */
public static void getProperties(){
Properties prop = System.getProperties();
Enumeration<?> emn = prop.propertyNames();
while(emn.hasMoreElements()){
Object o = emn.nextElement();
System.out.println(o.toString()+":"+prop.getProperty(o.toString()));
}
}
}