Java File canExecute() method with examples

January 04, 2020 No comments Java IO Example canExecute

1. Introduction

The canExecute() method tests if application can execute provided file. You shouldn't be relying on this method. Sometimes it will return true even though the file does not have permissions to execute, also JVM could be started with special privileges that allow it to execute files not marked executable.

2. Method signature

public boolean canExecute()


  • method does not take any parameter


  • true - if file is executeable and application can execute it


  • SecurityException - application does not have permission to the file

3. Examples

3.1. Check if file /tmp/ can be executed



public class FrontBackend {

    public static void main(String args[]) {

        try {
            File f = new File("/tmp/");
            boolean canExecute = f.canExecute();

            if (canExecute) {
                System.out.println("Application can execute the file");
            } else {
                System.out.println("Application cannot execute the file");

        } catch (Exception e) {

The output in a scenario when is executable in OS (used chmod +x filename to allow executable permissions):

Application can execute the file

4. Conclusion

In this article, we showcased how we could use File.canExecute() method to check if the given file can be executed by the application. Note that you shouldn't trust 100% of what this method returns, because specific parameters of a virtual Java machine will make applications able to execute a file that is not marked as executable.

{{ message }}

{{ 'Comments are closed.' | trans }}