Saturday, September 30, 2023

How to fix java.io.FileNotFoundException: (Access is denied)? Example

Earlier my impression was that java.io.FileNotFoundException: (Access is denied) comes when you try to read a text or binary file for which you don't have permission from the Java program but this can also come while you are using the jar command. jar command internally uses java.util.zip.ZipFile class to open any jar or war file which can throw java.io.FileNotFoundException: (Access is denied). I was trying to see the contents of the war file which was created using a recent build when I stumble upon this error, see the exact command below:

test@dev454:war-store/target jar -tvf maven-eclipse-web-demo
java.io.FileNotFoundException: maven-eclipse-web-demo (Access is denied)
        at java.util.zip.ZipFile.open(Native Method)
        at java.util.zip.ZipFile.<init>(ZipFile.java:127)
        at java.util.zip.ZipFile.<init>(ZipFile.java:88)
        at sun.tools.jar.Main.list(Main.java:979)
        at sun.tools.jar.Main.run(Main.java:224)
        at sun.tools.jar.Main.main(Main.java:1149)



Possible cause of java.io.FileNotFoundException: (Access is denied):

Here are some of the possible cause I checked immediately:



1. See if you have permission to read the jar file or not

this is the most obvious reason for java.io.FileNotFoundException: (Access is denied) exception and I didn't have sufficient permission so I immediately provided read, write and execute permission to war file and tried again
but it didn't help it was still throwing that exception.


2. See if some other process is using that file

This could be another possible reason for "java.io.FileNotFoundException: (Access is denied)". in my case I had earlier tried the "tar" command to read that file which I suspect hasn't been released the file. because after a few try it was solved.

Conclusion:

How to fix java.io.FileNotFoundException: (Access is denied)java.io.FileNotFoundException: (Access is denied) obviously shows something is wrong with file permissions. if you have sufficient file permission than possible reason both in Windows and Unix is might be some other process is using that file which may prevent jar command to list down contents of war file or extract content of war file. 

By the way if you are not aware of how to see contents of war file than its good to know that jar file can be used to see contents of both .jar and .war file. I haven't tested .ear file but I am sure jar command can extract that as well because they are kind of zip file.


Other Java troubleshooting tutorials you may like

Let me know if you have faced this issue and not able to solve it, happy to help. Please post the full error in comments and provide context like what you are doing and when you get this error, this will help me to solve your problem. 

2 comments :

Unknown said...

set EXTRA_JAVA_PROPERTIES=-Duser.language=en -Duser.country=US %EXTRA_JAVA_PROPERTIES%
set JAVA_PROPERTIES=%JAVA_PROPERTIES% %EXTRA_JAVA_PROPERTIES%

EN este archivo .- C:\Users\cjimenez.DGSGIF\AppData\Roaming\JDeveloper\system12.2.1.3.42.170820.0914\DefaultDomain\bin
setDomainEnv.cmd

Anonymous said...

I am getting FileNotFoundException, system cannot find the path, while runing tests with Maven, can you please advise? I have config files in folder under root maven project. Its a multi-module maven project and config file are under one module but maven cannot find and load them while running test and throwing this java.io.FileNotFoundException?

Post a Comment