Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java java.nio.file.Files isExecutable() Method

Java java.nio.file.Files.isExecutable() Method

Java File Java java.nio.file.Files

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

Return Value


Method details

Description

This method checks whether the JVM has sufficient permission to execute the specified file. Note that:

  1. On UNIX systems, this checks the file's execute permission bit
  2. On Windows systems, all files are considered "executable", so as long as the file exists, the method returns true
  3. 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:


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

  1. Platform differences: As mentioned above, Windows and UNIX systems have significant differences in their implementation of this method
  2. Symbolic links: If the path points to a symbolic link, the executability of the link target is checked
  3. Performance considerations: Frequent calls to this method may affect performance; cache the result when necessary
  4. 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 File Java java.nio.file.Files