package test; import java.io.BufferedReader; import java.io.InputStreamReader; public class ExcuteLinux { public static String exeCmd(String commandStr) { String result = null; try { String[] cmd = new String[]{"/bin/sh", "-c",commandStr}; Process ps = Runtime.getRuntime().exec(cmd); BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream())); StringBuffer sb = new StringBuffer(); String line; while ((line = br.readLine()) != null) { //执行结果加上回车 sb.append(line).append("\n"); } result = sb.toString(); } catch (Exception e) { e.printStackTrace(); } return result; }
public static void main(String[] args) {
//查询显卡型号,传入参数为需要执行的命令 String result = ExcuteLinux.exeCmd("lspci | grep -i vga");
//查询声卡型号 String result1 = ExcuteLinux.exeCmd("lspci | grep -i audio"); } }
个人学习随笔,若是帮助到您,请给个推荐,或者评论一下下,谢谢了哟。若有不懂之处,请联系QQ:351094262(验证信息:博客园),原创不易,转载请标明出处!