Jackson
Jackson

Reputation: 3518

AccessController checkPermission always throws exception

How do you check if the application has permission to read or write to specific directories on the filesystem. I am trying this:

try {
    AccessController.checkPermission(new FilePermission(files[i]
            .getAbsolutePath(), "read,write"));
}
catch (Exception e) {
    System.out.println(e.toString());
}

but it always throws the exception and outputs:

java.security.AccessControlException: access denied ("java.io.FilePermission" "C:\Dell" "read")
java.security.AccessControlException: access denied ("java.io.FilePermission" "C:\Documents and Settings" "read")
java.security.AccessControlException: access denied ("java.io.FilePermission" "C:\glut-3.7.6" "read")

Is there something I'm missing here?

Upvotes: 3

Views: 1331

Answers (2)

يعقوب
يعقوب

Reputation: 1222

Text from documentation :

 Determines whether the access request indicated by the
 specified permission should be allowed or denied, based on
 the security policy currently in effect, and the context in
 this object. The request is allowed only if every ProtectionDomain
 in the context implies the permission. Otherwise the request is
 denied.

Upvotes: 0

mishadoff
mishadoff

Reputation: 10789

Seems that AccessController always throws an exception when you don't have permission. You can handle this by catching SecurityException.

Better solution to check read/write permission with java.io.File.canRead(), java.io.File.canWrite() methods.

Upvotes: 1

Related Questions