11

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?

Sindri Traustason
  • 5,245
  • 6
  • 45
  • 65

3 Answers3

19

See java.io.File.canExecute()

wjans
  • 9,794
  • 5
  • 31
  • 39
4

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);
BarBQ
  • 41
  • 3
4

The class java.io.File has method canExecute().

ncmathsadist
  • 4,504
  • 3
  • 29
  • 44