java执行linux命令

列举个例子,其他类推:

1.java删除linux服务器某个目录下的文件夹

Process process =null;
//文件夹路径
String path = "/var/www/uploadFileResource/temp";
String[] cmd = new String[] { "/bin/sh", "-c", "rm -rf "+path };
try {
    //执行命令
    process = Runtime.getRuntime().exec(cmd);
    log.info("临时文件清理执行");
    InputStreamReader ips = new InputStreamReader(process.getInputStream());
    BufferedReader br = new BufferedReader(ips);
    String line;
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

你可能感兴趣的:(java,linux,java,运维)