80

For some reason I keep getting java.nio.file.AccessDeniedException every time I try to write to a folder on my computer using a java webapp on Tomcat. This folder has permissions set to full control for everyone on my computer (Windows). Does anybody know why I get this exception?

Here's my code:

public void saveDocument(String name, String siteID, byte doc[]) {
    try {
        Path path = Paths.get(rootDirectory + siteID);
        if (Files.exists(path)) {
            System.out.println("Exists: " + path.toString());
            Files.write(path, doc);
        } else {
            System.out.println("DOesn't exist");
            throw new Exception("Directory for Site with ID " + siteID + "doesn't exist");
        }
    } catch (FileSystemException e) {
        System.out.println("Exception: " + e);
        e.printStackTrace();
    } catch (IOException e ) {
        System.out.println("Exception: " + e);
        e.printStackTrace();
    } catch (Exception e) {
        System.out.println("Exception: " + e);
        e.printStackTrace();
    }

And here is the error:

Exception: java.nio.file.AccessDeniedException: C:\safesite_documents\site1 java.nio.file.AccessDeniedException: C:\safesite_documents\site1 at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:83) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102) at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230) at java.nio.file.spi.FileSystemProvider.newOutputStream(FileSystemProvider.java:430) at java.nio.file.Files.newOutputStream(Files.java:172) at java.nio.file.Files.write(Files.java:3092)

Possible reason why: See my post on supersuser about how I can't uncheck 'Read Only' for any of my folders on windows 7. Even though all the folders aren't read only to anything but java.

Serban Petrescu
  • 5,127
  • 2
  • 17
  • 34
OneTwo
  • 2,291
  • 6
  • 33
  • 55
  • try to write to folder in other drive and check. – Prashant Feb 23 '15 at 10:00
  • 1
    No it doesn't work with a different drive or a different folder. One thing it may be is that all my folders on my pc are marked as 'Read Only' Even though they aren't read only. And when I un-mark they just get checked again after. – OneTwo Feb 23 '15 at 10:06
  • @OneTwo, Is there any possibility that same path is being using by any other code. Like where you have accessed the same path and forget to release the resources. – Amogh Feb 23 '15 at 10:48
  • No I have tried writing a byte array in a test java main method, project with nothing else and I still get the same error. I have no other applications open which are accessing the folder as far as I can see. – OneTwo Feb 23 '15 at 10:51
  • 1
    I made same mistake, you must target the file not the folder. But why does it throw that exception? It has nothing to do with access of the file/folder, right? – Stefan Jankovic Jul 20 '20 at 11:54

7 Answers7

128

Ok it turns out I was doing something stupid. I hadn't appended the new file name to the path.

I had

rootDirectory = "C:\\safesite_documents"

but it should have been

rootDirectory = "C:\\safesite_documents\\newFile.jpg" 

Sorry it was a stupid mistake as always.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
OneTwo
  • 2,291
  • 6
  • 33
  • 55
  • 2
    This seems to be the main cause for `java.nio.file.AccessDeniedException`. It's hard to debug sometimes, but, if you get this, you should check all your system properties set with `java` and your config files – payloc91 Jul 04 '18 at 13:01
  • just sharing, mine is that the system properties do not allow my server "jboss" to write the file. +1 for @MarkoPacak – 薛源少 Aug 29 '18 at 06:51
  • 1
    Been a while since I did development on Windows like this, but I'm pretty sure you don't need \\ and can instead use / – Christopher Schneider Sep 06 '19 at 19:15
  • Thia can be a common mistake for many people who do operation on files. The best part is when we sort it out on our own! – arunken Aug 25 '22 at 05:54
3

Getting java.nio.file.AccessDeniedException when trying to write to a folder

Unobviously, Comodo antivirus has an "Auto-Containment" setting that can cause this exact error as well. (e.g. the user can write to a location, but the java.exe and javaw.exe processes cannot).

In this edge-case scenario, adding an exception for the process and/or folder should help.

Temporarily disabling the antivirus feature will help understand if Comodo AV is the culprit.

I post this not because I use or prefer Comodo, but because it's a tremendously unobvious symptom to an otherwise functioning Java application and can cost many hours of troubleshooting file permissions that are sane and correct, but being blocked by a 3rd-party application.

tresf
  • 7,103
  • 6
  • 40
  • 101
1

I was getting the same error when trying to copy a file. Closing a channel associated with the target file solved the problem.

Path destFile = Paths.get("dest file");
SeekableByteChannel destFileChannel = Files.newByteChannel(destFile);
//...
destFileChannel.close();  //removing this will throw java.nio.file.AccessDeniedException:
Files.copy(Paths.get("source file"), destFile);
ekene
  • 111
  • 8
1

Not the answer for this question

I got this exception when trying to delete a folder where i deleted the file inside.

Example:

createFolder("folder");  
createFile("folder/file");  
deleteFile("folder/file");  
deleteFolder("folder"); // error here

While deleteFile("folder/file"); returned that it was deleted, the folder will only be considered empty after the program restart.

On some operating systems it may not be possible to remove a file when it is open and in use by this Java virtual machine or other programs.

https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#delete-java.nio.file.Path-

Explanation from dhke

1

After long time to open my android project (Android Studio), and it getting same issue like above. And I solve it by "Clean Project". You Just go to menu "Build" > "Clean Project".

Dharman
  • 30,962
  • 25
  • 85
  • 135
Tomero Indonesia
  • 1,685
  • 2
  • 16
  • 17
0

Delete .android folder cache files, Also delete the build folder manually from a directory and open android studio and run again.

enter image description here

vinod
  • 1,126
  • 13
  • 18
0

I was seeing this error in my spring-boot(version : 2.6.2) project.

When I changed the version to 2.7.2 and re-built the project, the error went away.

    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.2</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent>
mnagdev
  • 384
  • 3
  • 11