Search This Blog

Breaking

Friday, 14 July 2023

July 14, 2023

Jenkins AccessDeniedException While Accessing Folder (Works Manually) – Fix

Recently, I encountered an issue where a Jenkins job failed with an AccessDeniedException while trying to access a folder on Windows.

What made it confusing was:

  • The same folder was accessible manually
  • The same code worked when run directly on the machine





Problem

When the Jenkins job tried to access a file inside a folder, it failed with:

AccessDeniedException

However:

  • Manual access → ✅ Working
  • Direct execution on machine → ✅ Working
  • Jenkins-triggered execution → ❌ Failing

Debugging Approach

We analyzed the issue step by step from multiple angles.


1. File Path Construction

We first checked how the file path was being created.

Why?
If Jenkins runs on Linux and your path is Windows-style, it can fail.

Best Practice:
Use Java’s file separator instead of hardcoding paths.

Example:

File.separator

Outcome:
The path was correct.


2. Folder Accessibility

Next, we verified whether the folder was accessible from the machine where Jenkins was running.

Why?
The folder was a shared location, so access permissions matter.

Outcome:
The folder was accessible.


3. Run Code Locally on Same Machine

We executed the same code directly on the Jenkins machine.

Why?
To rule out Java or dependency issues.

Outcome:
Code worked perfectly.


4. Narrowing Down the Issue

At this point:

  • Code works locally ✅
  • Works on Jenkins machine directly ✅
  • Fails only when triggered via Jenkins ❌

👉 This confirmed the issue is Jenkins execution context related, not code-related.


5. Check Jenkins User Permissions

We verified which user Jenkins was using to execute the job.

Why?
Jenkins jobs may run under a different system/service user.

Outcome:
The user appeared to be the same as the logged-in user.


6. The Unexpected Fix

As part of general troubleshooting, we restarted the machine.

Why?
We noticed that when giving folder access to users, a reboot was sometimes required.

Outcome:
After restart, the issue was resolved and access started working.


Final Observation

Even after fixing the issue, one question remained:

  • Why did the code work when run manually on the same machine?
  • But fail only when triggered via Jenkins?

Most likely reasons:

  • Cached permissions
  • Jenkins service session not refreshed
  • OS-level permission propagation delay

Key Takeaways

  • AccessDeniedException is often environment-related, not code-related
  • Jenkins execution context can differ from manual execution
  • Always check:
    • File paths
    • Permissions
    • Execution user
  • Sometimes, a simple system restart resolves hidden permission issues

Closing Thought

This issue looked simple at first but turned out to be tricky due to inconsistent behavior.

If you’ve faced something similar or know the exact root cause behind this behavior, feel free to share—would be interesting to understand it deeper.