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

1

20

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?

Hizar answered 19/1, 2013 at 15:42 Comment(0)
U
28

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");

Usia answered 19/1, 2013 at 15:47 Comment(5)
yes i want to know what should be the code for creating a directory in windows!!Hizar
Hey, don't recommend using File :(Diagnosis
and One more probably last one How to change the name of owner of file in the above code?? please reply!!Hizar
Hmm.. being honest I have never done this. Doesn't your previous code work (using 1.7 libraries)? If not you have to remember that e.g. FAT32 file system doesn't have something like 'fileowner'.Usia
ok please tell how to give a user Read/write access in FAT32 system in java if there is nothing like file owner.User name is rashmi and run that code as administrator it should work it is working on my system!!Hizar

© 2022 - 2024 — McMap. All rights reserved.