java Runtime如何执行多条命令
时间:2022-02-27 09:21:08|栏目:JAVA代码|点击: 次
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 && " );
栏 目:JAVA代码
下一篇:springboot+WebMagic+MyBatis爬虫框架的使用
本文标题:java Runtime如何执行多条命令
本文地址:http://www.codeinn.net/misctech/194655.html


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




