Java File canExecute() Method
Example
Turn on the execute permission of a file, then check if the file can be run:
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();
myFile.setExecutable(true);
System.out.println(myFile.canExecute());
}
}
Definition and Usage
The canExecute() method checks if the program is allowed to execute the file (run it as a program).
It returns false if the file does not exist. On Windows, Java does not check an execute permission, so the result is usually true for a file that exists. On Linux and macOS, the file needs the execute permission.
Tip: Use the setExecutable() method to change the execute permission.
Syntax
public boolean canExecute()
Parameter Values
None.
Technical Details
| Returns: | true if the file exists and can be executed, otherwise false. |
|---|
Related Pages
❮ File Methods