Java Files isExecutable() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Java 7+

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:

Java
boolean runnable = Files.isExecutable(dir.resolve("build.sh"));
CallWhat 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
Java
public static boolean isExecutable(Path path)

Parameters

ParameterDescription
pathThe file or folder to test.

Return Value

Return ValueDescription
trueIt exists and execute (or search) access is granted.
falseMissing, not executable, or unknown.

Compare with Similar Methods

Permission tools:

MethodHow it differs
setPosixFilePermissions()Turn x bits on or off (Linux/macOS).
isReadable()Read access instead of execute.
getPosixFilePermissions()See every bit at once.

Java Examples

Example 1 — Make a Shell Script Executable

A new script has no execute bit on Linux or macOS; setting rwxr-xr-x changes that.

Java
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);
    }
}

How It Works

  1. No x bit yet. Files are created without execute permission on Linux and macOS, so the first answer is false.
  2. The chmod 755 equivalent. rwxr-xr-x gives the owner all rights and everyone else read and execute; now the script may run.
  3. 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.

Java
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);
    }
}

How It Works

  1. Execute means search. A normal folder can be entered, so isExecutable(vault) is true on every system.
  2. Drop the x bit. With rw------- the folder can still be listed by name, but nothing inside can be opened or even checked.
  3. Consequence inside. isRegularFile(coin) cannot look up coin.txt and returns false, although the file still exists.
  4. 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.

Java
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);
    }
}

How It Works

  1. One file marked. setExecutable(true) adds the owner x bit to deploy.sh on Linux and macOS; notes.txt keeps the default bits.
  2. Filter and sort. Files::isExecutable keeps the runnable entries, and sorting fixes the order the listing returns them in.
  3. 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

Call Files.isExecutable(path). On Linux and macOS it checks the execute permission bits; on Windows it checks access rights.
Windows does not use execute bits. Files you create usually grant execute access, so the check passes regardless of the extension.
Use path.toFile().setExecutable(true), or on Linux and macOS Files.setPosixFilePermissions(path, PosixFilePermissions.fromString("rwxr-xr-x")).
Search permission: whether you may open entries inside it. Without it, even files you know the names of cannot be reached.
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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