Close

Java IO & NIO - Files.isExecutable() Examples

Java IO & NIO Java Java API 


Class:

java.nio.file.Files

java.lang.Objectjava.lang.Objectjava.nio.file.Filesjava.nio.file.FilesLogicBig

Method:

public static boolean isExecutable(Path path)

Tests whether a file is executable. It checks that the current user has executable permission for the provided file. If user does not have permission to execute the file it returns false, otherwise true.

Parameters:
path - the path to the file to check
Returns:
true if the file exists and is executable; false if the file does not exist, execute access would be denied because the Java virtual machine has insufficient privileges, or access cannot be determined


Examples


package com.logicbig.example.files;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class IsExecutableExample {

public static void main(String... args) throws IOException {
Path path = Paths.get("C:\\Windows\\System32\\notepad.exe");
System.out.println("checking: " + path);
boolean executable = Files.isExecutable(path);
System.out.println(executable);

path = Files.createTempFile("test", ".txt");
System.out.println("checking: " + path);
executable = Files.isExecutable(path);
System.out.println(executable);
}
}

Output

checking: C:\Windows\System32\notepad.exe
true
checking: C:\Users\Joe\AppData\Local\Temp\test6737629320399319755.txt
true




package com.logicbig.example.files;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.AclEntry;
import java.nio.file.attribute.AclEntryPermission;
import java.nio.file.attribute.AclEntryType;
import java.nio.file.attribute.AclFileAttributeView;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class IsExecutableExample2 {

public static void main(String... args) throws IOException {
Path path = Files.createTempFile("test", ".txt");
System.out.println(Files.isReadable(path));

//on windows remove execute permission
System.out.println("Denying execute permission to owner: " + path);
AclFileAttributeView view = Files.getFileAttributeView(path, AclFileAttributeView.class);
Set<AclEntryPermission> newPermissions =
new HashSet<>(Arrays.asList(AclEntryPermission.values()));
newPermissions.remove(AclEntryPermission.EXECUTE);
AclEntry entry = AclEntry.newBuilder()
.setType(AclEntryType.ALLOW)
.setPrincipal(Files.getOwner(path))
.setPermissions(newPermissions)
.build();
view.setAcl(Arrays.asList(entry));
//get new path
path = Paths.get(path.toString());
System.out.println("checking: " + path);
System.out.println("exits: " + Files.exists(path));
boolean executable = Files.isExecutable(path);
System.out.println("executable: " + executable);
}
}

Output

true
Denying execute permission to owner: C:\Users\Joe\AppData\Local\Temp\test15495899723461920102.txt
checking: C:\Users\Joe\AppData\Local\Temp\test15495899723461920102.txt
exits: true
executable: false




See Also