public class Test { public static Map<String, String> executeCmd(String cmd) { Runtime rt = Runtime.getRuntime(); // 运行时系统获取 Map<String, String> lineMap = new HashMap<String, String>();//存放返回值 try { Process proc = rt.exec(cmd);// 执行命令 InputStream stderr = proc.getInputStream();//执行结果 得到进程的标准输出信息流 InputStreamReader isr = new InputStreamReader(stderr);//将字节流转化成字符流 BufferedReader br = new BufferedReader(isr);//将字符流以缓存的形式一行一行输出 String line = null; while ((line = br.readLine()) != null) { if (!StringUtils.isEmpty(line)) { String[] strLine = line.split(":"); if(strLine.length>=2) { lineMap.put(strLine[0].trim(), strLine[1].trim()); } } } br.close(); isr.close(); stderr.close(); } catch (IOException e) { e.printStackTrace(); } return lineMap; } public static void main(String []args){ executeCmd("mstsc"); } }
原文:https://www.cnblogs.com/baizhuang/p/11298570.html