Home Java Files Methods isExecutable() Java Files isExecutable() Method Definition In Java, Files.isExecutable(path) returns true when the file exists and the current user is allowed to execute it. For a folder, "execute" means search : permission to open names inside it.
The meaning depends on the system. On Linux and macOS it follows the POSIX x bits (chmod +x). On Windows it follows access rights, which usually allow execute for files you create, whatever the extension.
A static method of the final utility class java.nio.file.Files, available since Java 7. Call it on the class name, as in Files.isExecutable(...), after import java.nio.file.Files;. You never create a Files object.
Syntax Pass the path; there are no options:
boolean runnable = Files.isExecutable(dir.resolve("build.sh")); Call What it does Files.isExecutable(p)May execute (file) or search (folder)? p.toFile().setExecutable(true)Adds the owner x bit on Linux and macOS. Files.setPosixFilePermissions(p, set)Sets all bits, e.g. rwxr-xr-x.
Since Java 7. The check says nothing about whether the file is a real program; a text file with the x bit set is "executable" too.
Library declarations public static boolean isExecutable(Path path) Parameters Parameter Description pathThe file or folder to test.
Return Value Return Value Description trueIt exists and execute (or search) access is granted. falseMissing, not executable, or unknown.
Important
No checked exception. New files on Linux and macOS normally have no x bit, so they start as not executable; on Windows the same file is typically executable. setPosixFilePermissions throws UnsupportedOperationException on Windows, so the examples check for POSIX support first.
Compare with Similar Methods Permission tools:
Java Examples 1. chmod 755 a script 2. Folders and the x bit 3. Find runnable files Example 1 — Make a Shell Script Executable A new script has no execute bit on Linux or macOS; setting rwxr-xr-x changes that.
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermissions;
public class IsExecutableScript {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path script = dir.resolve("build.sh");
Files.write(script, "#!/bin/sh\necho built\n".getBytes(StandardCharsets.UTF_8));
System.out.println("Executable at first: " + Files.isExecutable(script));
if (FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) {
Files.setPosixFilePermissions(script, PosixFilePermissions.fromString("rwxr-xr-x"));
System.out.println("After rwxr-xr-x: " + Files.isExecutable(script));
} else {
System.out.println("Windows uses access rights, not x bits");
}
Files.delete(script);
Files.delete(dir);
}
} Run with java IsExecutableScript. The first part is the Linux/macOS output; the last part is typical for Windows.
Executable at first: false
After rwxr-xr-x: true
--- On Windows (typical) ---
Executable at first: true
Windows uses access rights, not x bits How It Works No x bit yet. Files are created without execute permission on Linux and macOS, so the first answer is false.The chmod 755 equivalent. rwxr-xr-x gives the owner all rights and everyone else read and execute; now the script may run.Windows answers differently. It grants execute access to files you create in your own folders, so the first check is typically already true.Example 2 — A Folder Without x Cannot Be Entered On Linux and macOS remove execute from a folder: it stops being "executable", and files inside become unreachable.
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermissions;
public class IsExecutableFolder {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path vault = Files.createDirectory(dir.resolve("vault"));
Path coin = Files.createFile(vault.resolve("coin.txt"));
System.out.println("vault searchable: " + Files.isExecutable(vault));
if (FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) {
Files.setPosixFilePermissions(vault, PosixFilePermissions.fromString("rw-------"));
System.out.println("without x: " + Files.isExecutable(vault));
System.out.println("coin.txt reachable: " + Files.isRegularFile(coin));
Files.setPosixFilePermissions(vault, PosixFilePermissions.fromString("rwx------"));
} else {
System.out.println("No POSIX permissions on this system");
}
Files.delete(coin);
Files.delete(vault);
Files.delete(dir);
}
} Run with java IsExecutableFolder. The first part is the output on Linux or macOS for a normal user (not root); the last part shows Windows, which has no POSIX permissions.
vault searchable: true
without x: false
coin.txt reachable: false
--- On Windows ---
vault searchable: true
No POSIX permissions on this system How It Works Execute means search. A normal folder can be entered, so isExecutable(vault) is true on every system.Drop the x bit. With rw------- the folder can still be listed by name, but nothing inside can be opened or even checked.Consequence inside. isRegularFile(coin) cannot look up coin.txt and returns false, although the file still exists.Restore, then clean. rwx------ gives access back so the files can be deleted.Example 3 — List the Runnable Files in a bin Folder Mark one file executable with File.setExecutable and list which entries isExecutable accepts.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class IsExecutableRunnable {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path deploy = Files.createFile(dir.resolve("deploy.sh"));
Path notes = Files.createFile(dir.resolve("notes.txt"));
deploy.toFile().setExecutable(true);
List<String> runnable;
try (Stream<Path> entries = Files.list(dir)) {
runnable = entries.filter(Files::isExecutable)
.map(p -> p.getFileName().toString())
.sorted()
.collect(Collectors.toList());
}
System.out.println("Runnable: " + runnable);
Files.delete(deploy);
Files.delete(notes);
Files.delete(dir);
}
} Run with java IsExecutableRunnable. The first part is the Linux/macOS output; the last part is typical for Windows.
Runnable: [deploy.sh]
--- On Windows (typical) ---
Runnable: [deploy.sh, notes.txt] How It Works One file marked. setExecutable(true) adds the owner x bit to deploy.sh on Linux and macOS; notes.txt keeps the default bits.Filter and sort. Files::isExecutable keeps the runnable entries, and sorting fixes the order the listing returns them in.Same code, different answer. On Windows both files usually pass, because execute access does not depend on an x bit or the extension.Frequently Asked Questions How do I check if a file is executable in Java? Call Files.isExecutable(path). On Linux and macOS it checks the execute permission bits; on Windows it checks access rights.
Why does isExecutable return true for a .txt file on Windows? Windows does not use execute bits. Files you create usually grant execute access, so the check passes regardless of the extension.
How do I make a file executable in Java? Use path.toFile().setExecutable(true), or on Linux and macOS Files.setPosixFilePermissions(path, PosixFilePermissions.fromString("rwxr-xr-x")).
What does isExecutable mean for a directory? Search permission: whether you may open entries inside it. Without it, even files you know the names of cannot be reached.
Does isExecutable check that the file is a real program? No. It checks permissions only, not the file content.
Next: Files.isHidden() Check whether a file counts as hidden: a dot name on Linux/macOS, the hidden attribute on Windows.
isHidden() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
7 people found this page helpful
Helpful Share Copy link Suggestion