java Runtime.getRuntime().exec 调用系统脚本/命令注意事项

错误的方法:

//CPUID
private static final String cpuid="dmidecode -t processor | grep 'ID' | head -1";

Process p = Runtime.getRuntime().exec(cpuid);

原因:不会被再次解析,管道符失效

 

正确的办法:

linux下:

String[] command = { "/bin/sh", "-c", cpuid };

Process ps = Runtime.getRuntime().exec(command );

windows下:

String[] command = { "cmd", "/c", cpuid };

Process ps = Runtime.getRuntime().exec(command );

你可能感兴趣的:(java Runtime.getRuntime().exec 调用系统脚本/命令注意事项)