Home » Java programming language

Java File Class boolean canExecute() method with Example

Java File Class boolean canExecute() method: Here, we are going to learn about the boolean canExecute() method of File class with its syntax and example.
Submitted by Preeti Jain, on July 06, 2019

File Class boolean canExecute()

  • This method is available in package
  • This method is used to execute the file and the file is represented by the abstract file path or in other words this method is used to test whether the application can execute the file or not.
  • The return type of this method is Boolean i.e. it returns true or false if true that means file can be executed by the application which is represented by the file path or in other words file already exists to execute and returns false that means file does not exist it means the application is not allowed to execute the file.
  • This method may raise an exception( i.e. Security Exception) if the execute access is not given to the file.


    boolean canExecute(){


We don't pass any object as a parameter in the method of the File.

Return value:

The return type of this method is Boolean i.e. it returns true if the file already exists and allowed to execute the file which is denoted by the abstract file path returns false otherwise.

Java program to demonstrate example of canExecute() method

// import the File class because we will use File class methods

//import the Exception class because it may raise 
// an exception when working with files
import java.lang.Exception;

public class ExecuteFile {
    public static void main(String[] args) {
        try {
            // Specify the path of file and we use double slashes 
            // to escape '\' character sequence for windows otherwise 
            // it will be considerable as url.
            File file1 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava.txt");
            File file2 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\java.txt");

            // By using canExecute() is allowed to execute 
            // the file if file is already exists and 
            // it returns true if file is executable else false returns.
            if (file1.canExecute())
                System.out.println("This file " + file1.getName() + " " + "is executable");
                System.out.println("This file " + file1.getName() + " " + "is not executable");

            // By using canExecute() is not allowed to execute 
            // the file because this file is not already exists 
            // and it returns false.
            if (file2.canExecute())
                System.out.println("This file " + file2.getName() + " " + "is executable");
                System.out.println("This file " + file2.getName() + " " + "is not executable");
        } catch (Exception e) {
            System.out.println("An error occurred.");


D:\Programs>java ExecuteFile

This file C:\Users\computer clinic\OneDrive\Articles\myjava.txt is not executable
This file C:\Users\computer clinic\OneDrive\Articles\java.txt is not executable


Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.