Java java.nio.file.Files.isExecutable() Method
isExecutable() The method is java.nio.file.Files class, used to check whether the Java virtual machine has permission to execute the specified file.
Method Definition
public
static
boolean
isExecutable
(
Path path
)
Parameters
path: The file path to check, of typejava.nio.file.Path
Return Value
boolean: If the file exists and is executable, returnstrue; otherwise it returnsfalse
Method details
Description
This method checks whether the JVM has sufficient permission to execute the specified file. Note that:
- On UNIX systems, this checks the file's execute permission bit
- On Windows systems, all files are considered "executable", so as long as the file exists, the method returns
true - If the file does not exist or cannot be accessed (due to insufficient permissions), the method returns
false
Exception handling
This method may throw the following exceptions:
SecurityException: If a security manager is installed and denies access to the file
Usage examples
Basic example
Example
import
java.nio.file.*
;
public
class
IsExecutableExample
{
public
static
void
main
(
String
[
]
args
)
{
Path filePath
=
Paths.
get
(
"/usr/bin/ls"
)
;
// ls command on UNIX systems
if
(
Files.
isExecutable
(
filePath
)
)
{
System
.
out
.
println
(
"File is executable"
)
;
}
else
{
System
.
out
.
println
(
"File is not executable or does not exist"
)
;
}
}
}
Cross-platform example
Example
import
java.nio.file.*
;
public
class
CrossPlatformExample
{
public
static
void
main
(
String
[
]
args
)
{
// Windows example
Path windowsExe
=
Paths.
get
(
"C:\Windows\System32\cmd.exe"
)
;
// Linux/macOS example
Path unixScript
=
Paths.
get
(
"/usr/local/bin/myscript.sh"
)
;
checkExecutable
(
windowsExe
)
;
checkExecutable
(
unixScript
)
;
}
private
static
void
checkExecutable
(
Path path
)
{
System
.
out
.
println
(
"Check: "
path
)
;
System
.
out
.
println
(
"Executable: "
Files.
isExecutable
(
path
)
)
;
System
.
out
.
println
(
"Exists: "
Files.
exists
(
path
)
)
;
System
.
out
.
println
(
)
;
}
}
Practical Application Scenarios
Scenario 1: Verify script executability
Before executing an external script or program, first verify whether it is executable:
Example
public
void
runScript
(
Path scriptPath
)
throws
IOException
{
if
(
!
Files.
isExecutable
(
scriptPath
)
)
{
throw
new
IOException
(
"Script is not executable: "
scriptPath
)
;
}
// Logic to execute the script
ProcessBuilder pb
=
new
ProcessBuilder
(
scriptPath.
toString
(
)
)
;
pb.
start
(
)
;
}
Scenario 2: Security check
In applications with high security requirements, checking file executability can serve as part of security verification:
Example
public
boolean
isSafeToExecute
(
Path filePath
)
{
return
Files.
isRegularFile
(
filePath
)
&&
Files.
isReadable
(
filePath
)
&&
Files.
isExecutable
(
filePath
)
;
}
Notes
- Platform differences: As mentioned above, Windows and UNIX systems have significant differences in their implementation of this method
- Symbolic links: If the path points to a symbolic link, the executability of the link target is checked
- Performance considerations: Frequent calls to this method may affect performance; cache the result when necessary
- Security restrictions: When a security manager is present, additional permissions may be required to call this method
Summary
Files.isExecutable() is a simple but practical method, especially suitable for scenarios that need to verify file executability. Understanding its behavioral differences across platforms is important for writing cross-platform code. In practical applications, this method usually needs to be combined with other file-checking methods (such as exists(), isRegularFile() , etc.) for comprehensive verification.
Java java.nio.file.Files