Java
Kafka
Windows
Error Solving
java.nio.file.AccessDeniedException

Fix java.nio.file.AccessDeniedException in Kafka on Windows

Codemia Knowledge Hub ·

On Windows, Kafka throws java.nio.file.AccessDeniedException when its process cannot access or modify a file or directory: usually because the account running Kafka lacks permissions on the log directory, or because another process has the file locked. Fix the permissions, find what holds the lock, and check the directory paths in your configuration.

Where the exception comes from

The exception is thrown by Java's NIO file package, which Kafka uses for file operations. It indicates a permission problem at the operating system level.

It typically appears in three situations:

  1. Log directory access: Kafka stores commit logs in log directories. If the account running Kafka lacks read/write permission, it cannot use them.
  2. Temp directory use: Kafka and the Java runtime may use temporary directories for operations like log compaction, and access problems there also cause the exception.
  3. Locked files: on Windows, other processes can lock files even when permissions are correct. Antivirus programs and files left open in other programs are common culprits.

How to fix it

  1. Check and fix folder permissions:
    • Right-click the folder (for example the Kafka log directory) and select Properties.
    • On the Security tab, check that the user running Kafka has Full control or at least Modify.
    • If not, click Edit to change the permissions.
  2. Run Kafka as administrator:
    • Right-click the Kafka start script and choose "Run as administrator."
    • Running software as administrator carries security risks.
  3. Find processes locking the files:
    • Use a tool like Process Explorer to identify and stop processes that lock Kafka's files.
    • Make sure no unnecessary software is accessing Kafka's directories.
  4. Check Kafka and ZooKeeper configuration:
    • Confirm that Kafka's server.properties and ZooKeeper's zoo.cfg point to the right directories with the right permissions.

Reading the Kafka logs

Kafka's logs, often in the ./logs/ directory, show what operation was attempted and what was denied. An entry might look like:

 
[2023-03-15 19:05:43,495] ERROR Failed to start Kafka due to an uncaught exception (org.apache.kafka.common.KafkaException: Failed to access log directory ...)

Preventing it from coming back

  • Recheck permissions after updates or configuration changes.
  • Periodically check for permission issues and lock conflicts.
  • Run Kafka under a dedicated user account with only the permissions it needs.
Issue ComponentProblem DescriptionSolution/Action
PermissionInsufficient file/directory permissionsAdjust permissions, run Kafka as administrator
Locked FilesFiles locked by other Windows processesUse Process Explorer to unlock files
ConfigurationIncorrect paths or settings in configurationVerify server.properties and zoo.cfg files

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design