java Runtime如何执行多条命令
更新时间:2021年11月03日 09:15:38 作者:积极流年
这篇文章主要介绍了java Runtime如何执行多条命令,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
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操作系统");
}
}
Runtime.getRuntime().exec 执行多条
中间加上 & 或者 && 就可以执行多条了.
Runtime.getRuntime().exec("cmd1 && " +
"cmd2 && " +
"cmd3 && " );
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
微信js sdk invalid signature签名错误问题的解决方法分析
这篇文章主要介绍了微信js sdk invalid signature签名错误问题的解决方法,结合实例形式分析了微信签名错误问题相关解决方法,需要的朋友可以参考下2019-04-04
springboot 使用yml配置文件自定义属性的操作代码
在SpringBoot中yml/yaml文件可以自定义一些属性,以供注入给自定义bean对象的属性,主要通过空格和层次来实现,类似于python代码,本文通过实例代码给大家介绍springboot 使用yml配置文件自定义属性,感兴趣的朋友跟随小编一起看看吧2024-03-03


最新评论