File setExecutable() method in Java with Examples

The setExecutable() method is a part of File class. The function sets the owner’s or everybody’s permission to execute the abstract pathname.
The function is an overloaded function One function requires two parameters and the other only one.

Function Signature:

public boolean setExecutable(boolean a, boolean b)
public boolean setExecutable(boolean a)

Function Syntax:



file.setExecutable(boolean a, boolean b)
file.setExecutable(boolean a)

Parameters: The function is a overloaded function:

  • For the first overload:
    • If a true value is passed as the first parameter then it is allowed to execute the abstract pathname, else it is not allowed to execute the file.
    • If a true value is passed as the second parameter then the execution permission applies to the owner only, else it applies to everyone

    For the second overload:

  • If a true value is passed as a parameter then it is allowed to execute the abstract pathname, else it is not allowed to execute the file.

Return value: This function returns boolean value, whether the operation succeeded or not.

Exception: This method throws Security Exception if the function is not allowed write access to the file.

Below programs will illustrate the use of the setExecutable() function

Example 1: We will try to change the setExecutable permission of the owner of an existing file in f: directory.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the
// use of setExecutable() function
  
import java.io.*;
  
public class solution {
    public static void main(String args[])
    {
  
        // try-catch block to handle exceptions
        try {
  
            // Create a file object
            File f = new File("f:\\program.txt");
  
            // Check if the Executable permission
            // can be set to new value
            if (f.setExecutable(true)) {
  
                // Display that the Executable permission
                // is be set to new value
                System.out.println("Executable permission"
                                   + " is set");
            }
            else {
  
                // Display that the Executable permission
                // cannot be set to new value
                System.out.println("Executable permission"
                                   + " cannot be set");
            }
        }
        catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
}

chevron_right


Output:

Executable permission is set

Example 2: We will try to change the setExecutable permission of every one of an existing file in f: directory.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the
// use of setExecutable() function
  
import java.io.*;
  
public class solution {
    public static void main(String args[])
    {
  
        // try-catch block to handle exceptions
        try {
  
            // Create a file object
            File f = new File("f:\\program.txt");
  
            // Check if the Executable permission
            // can be set to new value
            if (f.setExecutable(true, false)) {
  
                // Display that the Executable permission
                // is be set to new value
                System.out.println("Executable permission"
                                   + " is set");
            }
            else {
  
                // Display that the Executable permission
                // cannot be set to new value
                System.out.println("Executable permission"
                                   + " cannot be set");
            }
        }
        catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
}

chevron_right


Output:

Executable permission is set

The programs might not run in an online IDE. please use an offline IDE and set the Parent file of the file



My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.