Search This Blog

Breaking

Friday, 14 July 2023

July 14, 2023

Access Denied Exception [java.nio.file.AccessDeniedException] [Debug/Fix] while accessing file data from java

 Recently, I got an issue where when I was trying to access file inside a folder in windows with Jenkin Job, I got "AccessDeniedException". Surprisingly, the folder was accessible when checked manually.

To debug the issue, we tried multiple aspects. For example:

1. We checked how we are creating folder/file path.

-> Reason for this :  If the Jenkin machine where the execution happening is "Linux", then it might be the path which works fine in windows will not work in Linux.

The best way to construct your path for files and directories in java is to use : File Sperator

(https://stackoverflow.com/questions/36663023/java-file-path-windows-linux)


Outcome: The path was correct.

2. After ensuring that the path was correct, next we tried to check that wheather from the system where execution is happening, that folder is accessible or not.

-> Reason for this: As it was a shared location, so it is important to check that the path should be accessible.

Outcome: We ensured that the folder is accessible from that machine.


3. The next analysis, brought us to check , if we ran the code directly in the machine, where issue is occurring, what will be the output?


Reason for this: To ensure, that there is not java, dependency issue.

Outcome: Code worked fine from that location.


It leads to a conclusion that, there is no issue in the code, the issue comes only when the execution happens via Jenkin.

4. We have narrowed down the analysis now, but it looked more complex. The code was working fine in local machine, it was working fine when we were running it from the Jenkins machines directly, but only fails when we trigger the execution from the Jenkins

5. We checked the user now from which the execution is triggering from Jenkins.

Reasons for this: To check weather the user is having rights to that folder or not.

Output: User was same with which we logged to that machine. This made it more complex.

6. Restart the machine where execution was happening

Reason for this: It came out just as a normal discussion. When we were giving access to anyone to that folder, we were rebooting our system too.

Outcome: The access started working.

I still could not figure out , that why the code was working fine when we were running it from the same machine and why it failed, when we were triggering it from Jenkins.

That analysis I will do on my free time, but if you are reading this article, and if you have any idea about it, feel free to comment. It would be interesting to know.