java Runtime 执行多条命令
java Runtime 执行多条命令:public static void cmd(){String cmd_linux = "/bin/sh -ccd /home/ &&ls ";String cmd_windows = "cmd /ccdD:/home &&dir ";Process process = null;String cmd = "";swi
·
java Runtime 执行多条命令:使用 && 分隔命令
public static void cmd() {
String ls = " cd /home/ && dir ";
Process process = null;
String cmd = getOsCmd()+ ls;
try {
process = Runtime.getRuntime().exec(cmd);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(new String(line.getBytes(),"GBK"));
}
}catch (Exception e){
e.printStackTrace();
}
finally {
process.destroy();
}
}
public static String getOsCmd(){
Properties props=System.getProperties(); //获得系统属性集
String osName = props.getProperty("os.name"); //操作系统名称
if(osName.toLowerCase().contains("linux")){
return "/bin/sh -c";
}else if(osName.toLowerCase().contains("windows")){
return "cmd /c";
}else{
throw new RuntimeException("服务器不是linux|windows操作系统");
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)