java.lang.UnsupportedOperationException: 'posix:permissions' not supported as initial attribute on Windows

Ritesh Mehandiratta picture Ritesh Mehandiratta · Jan 19, 2013 · Viewed 19.9k times · Source

I am using Java 7 File API. I wrote a class that is working fine on Ubuntu creating directories perfectly, but when I run same code on Windows then it is throwing error:

Exception in thread "main" java.lang.UnsupportedOperationException: 'posix:permissions' not supported as initial attribute
    at sun.nio.fs.WindowsSecurityDescriptor.fromAttribute(Unknown Source)
    at sun.nio.fs.WindowsFileSystemProvider.createDirectory(Unknown Source)
    at java.nio.file.Files.createDirectory(Unknown Source)
    at java.nio.file.Files.createAndCheckIsDirectory(Unknown Source)
    at java.nio.file.Files.createDirectories(Unknown Source)
    at com.cloudspoke.folder_permission.Folder.createFolder(Folder.java:27)
    at com.cloudspoke.folder_permission.Main.main(Main.java:139)

My Folder class code is

package com.cloudspoke.folder_permission;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.UserPrincipal;
import java.util.Set;

public class Folder{
    // attributes required for creating a Folder
    private UserPrincipal owner;
    private Path folder_name;
    private FileAttribute<Set<PosixFilePermission>> attr;


    public Folder(UserPrincipal owner,Path folder_name,FileAttribute<Set<PosixFilePermission>> attr){
        this.owner=owner;
        this.folder_name=folder_name;
        this.attr=attr;
    }
    //invoking this method will create folders
    public  void createFolder(){
        try {
            //createDirectories function is used for overwriting existing folder instead of createDirectory() method
            Files.createDirectories(folder_name, attr);
            Files.setOwner(folder_name, owner);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("created Folder "+this.folder_name);

    }
}

The error is coming from createFolder method of Folder.

How do I resolve this error?

Answer

Adam Sznajder picture Adam Sznajder · Jan 19, 2013

You use PosixFilePermission which can be used only with operating systems which are compatibile with POSIX:

A file attribute view that provides a view of the file attributes commonly associated with files on file systems used by operating systems that implement the Portable Operating System Interface (POSIX) family of standards.

Operating systems that implement the POSIX family of standards commonly use file systems that have a file owner, group-owner, and related access permissions. This file attribute view provides read and write access to these attributes`

Windows unfortunatelly doesn't support POSIX file systems so this is why your code doesn't work. In order to create a directory in Windows you should use:

new File("/path/to/folder").mkdir();

The / will be automatically changed to \ in Windows. If you want to create the whole path at once you have to use mkdirs() method. More info: http://docs.oracle.com/javase/6/docs/api/java/io/File.html

In order to set file permissions in Windows you have to use setReadable(), setWritable() and setExecutable(). That are File class methods and set only file owner's permissions. Note that mentioned methods were added in Java 1.6. In older versions you would have to use (Windows version):

Runtime.getRuntime().exec("attrib -r myFile");