Raj Pannala
Raj Pannala

Reputation: 67

Files.isExecutable(...) giving false though i have permission to the file

I was using java.nio.file.Files class to check the login user has permission to execute file. Though i have permission to execute file (i.e i can run that file manually), isExecutable(pathToFile) returning false. does JVM don't have permission to execute the file?

Upvotes: 0

Views: 215

Answers (1)

Holger
Holger

Reputation: 298233

The Files.is… convenience methods like Files.isExecutable(path) are good, if a simple true/false answer is sufficient, in which case they spare you from dealing with exception handling, but not much helpful when you want to know details. If you retrace what will happen in the background, you can catch the exception yourself to find out the reason why the JVM assumes that executing the file won’t work:

try {
    path.getFileSystem().provider().checkAccess(path, AccessMode.EXECUTE);
    System.out.println("can execute "+path);
} catch(IOException ex) {
    System.out.println("can not execute: "+ex);
}

The exception (or its actual type) might tell you about the reasons.

Note that this is an IOException. Contrary to the comments on your question, you can preclude a security policy issue. If there was a JVM security policy forbidding the access, the SecurityException was also be thrown when calling Files.isExecutable(path), which you had noticed.

If Files.isExecutable(path) returns false, you can safely assume that an IOException has been thrown by checkAccess, either, because it was assumed that the current user indeed has not enough access rights or because there was an I/O issue before getting to consider the access rights.

Upvotes: 1

Related Questions