Search code examples
javapermissions777

recursively set 777 permissions in java without using NIO


Is there a way to set 777 permissions on a path recursively(so that all directories on the path get 777 permissions) without using NIO.

We can do it for the last leaf of a given file by methods in the file class like below

import java.io.File;
import java.nio.file.FileSystems;


public class permissionTest {

    public static void main(String [] args){
        String dir = "./leaf1/leaf2/leaf3";

        File baseDir = new File(dir);

        boolean success = baseDir.mkdirs();
        if (success) {
            System.out.println("Created dirs");
            baseDir.setExecutable(true, false);
            baseDir.setReadable(true, false);
            baseDir.setWritable(true, false);
        }
        else{
            System.out.println("Not created");
        }

    }

}

the above gives 777 to leaf3 , how to give 777 to leaf1 & leaf2 as well? In a single command?


Solution

  • Process p;
            try {
                p = Runtime.getRuntime().exec("chmod 777 -R ./leaf1");
                p.waitFor();
    
            } catch (Exception e) {
                e.printStackTrace();
            }