Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a good way to determine if a file is executable in Java

Tags:

java

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?

like image 352
Sindri Traustason Avatar asked Jul 13 '11 13:07

Sindri Traustason


3 Answers

See java.io.File.canExecute()

like image 110
wjans Avatar answered Oct 12 '22 10:10

wjans


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

like image 39
ncmathsadist Avatar answered Oct 12 '22 12:10

ncmathsadist


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);
like image 39
BarBQ Avatar answered Oct 12 '22 10:10

BarBQ