I'm aware you can call
Runtime.getRuntime().exec(file);
and get an exception if it is not executable, however this is unsafe since running an executable can have side effects.
I guess just checking the extension is enough on Windows, but is there a way I can read the executable bit on *nix file systems?
What is the best way to find out if an file is executable in the OS?
See java.io.File.canExecute()
The class java.io.File
has method canExecute()
.
You can use Files
class.
It has this method static boolean isExecutable(Path path)
.
If you have just a String
of path then you can get Path
using Paths
class. Like this
Path path = Paths.get(pathToFile);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With