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");
}
}