如何使用Java更改Linux中的文件夹所有者?

时间:2017-12-07 10:38:05

标签: java path permissions file-permissions permission-denied

我试过

Path path = Paths.get("/home/HP-124/Desktop/testDir2");
String newOwn = "jack";
Process process = Runtime.getRuntime().exec("chown -R" +newOwn+ path);

File sourceFolder = new File("/home/HP-124/Desktop/testDir2");
UserPrincipal SourceFolderOwner = 
      java.nio.file.Files.getOwner(sourceFolder.toPath());
Files.setOwner(path,DestinationFolderOw);

错误

Exception in thread "main" java.nio.file.FileSystemException: /home/p.szwajkowsk/Desktop/testDir2: Operation not permitted
    at sun.nio.fs.UnixException.translateToIOException(UnixException.java:91)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
    at sun.nio.fs.UnixFileAttributeViews$Posix.setOwners(UnixFileAttributeViews.java:252)
    at sun.nio.fs.UnixFileAttributeViews$Posix.setOwner(UnixFileAttributeViews.java:274)
    at sun.nio.fs.FileOwnerAttributeViewImpl.setOwner(FileOwnerAttributeViewImpl.java:100)
    at java.nio.file.Files.setOwner(Files.java:2127)
    at com.javatutorialhq.tutorial.FilePermissions.main(FilePermissions.java:39)

0 个答案:

没有答案