Java执行Shell脚本

String[] cmd = new String[]{"sh", "-c", shell命令};
System.out.println(cmd.toString());
Process process = null;
try {
    process = Runtime.getRuntime().exec(cmd);
    //必须等待该进程结束,否则时间设置就无法生效
    process.waitFor();
} catch (Exception error) {
    error.printStackTrace();
    LOG.error(error.getMessage());
}finally{
    if(process!=null){
        process.destroy();
    }
}

 

你可能感兴趣的:(Java)