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,445
  • 6
  • 48
  • 66

3 Answers3

20

See java.io.File.canExecute()

wjans
  • 10,009
  • 5
  • 32
  • 43
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,684
  • 3
  • 29
  • 45