Java File setExecutable() Method
Example
Turn on the execute permission of a file, then check it:
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
File myFile = new File("filename.txt");
myFile.createNewFile();
System.out.println(myFile.setExecutable(true));
System.out.println(myFile.canExecute());
}
}
Definition and Usage
The setExecutable() method turns the execute permission on or off. The execute permission decides if a file can be run as a program.
On some systems, like Windows, Java cannot turn the execute permission off. There, setExecutable(false) returns false.
Tip: Use the canExecute() method to check if the file can be run.
Syntax
One of the following:
public boolean setExecutable(boolean executable)
public boolean setExecutable(boolean executable, boolean ownerOnly)
Parameter Values
| Parameter | Description |
|---|---|
| executable | Required. true to allow running the file, false to stop it. |
| ownerOnly | Optional. If true (the default), the permission is changed only for the owner of the file. If false, it is changed for all users. If the system cannot tell the owner from other users, it is changed for all users. |
Technical Details
| Returns: | true if the permission was changed, otherwise false. |
|---|
Related Pages
❮ File Methods