Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileNotFound (Access is denied) Exception on java.io

Why do I get this error when I run this program? This occurs after random iterations. Usually after the 8000th iteration.

public static void main(String[] args)
{
    FileWriter writer = null;
    try
    {
        for(int i = 0; i < 10000; i++)
        {
            File file = new File("C:\\Users\\varun.achar\\Desktop\\TODO.txt");

            if(file.exists())
            {
                System.out.println("File exists");
            }
            writer = new FileWriter(file, true);
            writer.write(i);
            System.out.println(i);
            writer.close();
            if(!file.delete())
            {
                System.out.println("unable to delete");
            }

            //Thread.sleep(10);
            //writer = null;
            //System.gc();
        }
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        if(writer != null)
        {
            try
            {
                writer.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
    }
}

After the exception occurs, the file isn't present. That means the it is deleting, but FIleWriter tries to acquire the lock before that, even though it isn't a multi threaded program. Is it because the Windows isn't deleting the file fast enough, and hence the FileWriter doesn't get a lock? If so, then file.delete() method returns before windows actually deletes it?

How do i resolve it, since i'm getting a similar issue during load testing my application.

EDIT 1: Stacktrace:

java.io.FileNotFoundException: C:\Users\varun.achar\Desktop\TODO.txt (Access is denied)     
at java.io.FileOutputStream.openAppend(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:192)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:116)
    at java.io.FileWriter.<init>(FileWriter.java:61)

EDIT 2 : Added file.exists() and file.delete conditions in the program. and the new stacktrace:

7452
java.io.FileNotFoundException: C:\Users\varun.achar\Desktop\TODO.txt (Access is denied)
    at java.io.FileOutputStream.openAppend(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:192)
    at java.io.FileWriter.<init>(FileWriter.java:90)
    at com.TestClass.main(TestClass.java:25)

EDIT 3 Thread dump

TestClass [Java Application]    
    com.TestClass at localhost:57843    
        Thread [main] (Suspended (exception FileNotFoundException)) 
            FileOutputStream.<init>(File, boolean) line: 192    
            FileWriter.<init>(File, boolean) line: 90   
            TestClass.main(String[]) line: 24   
    C:\Users\varun.achar\Documents\Softwares\Java JDK\JDK 6.26\jdk\jre\bin\javaw.exe (09-Nov-2011 11:57:34 PM)  

EDIT 4 : Program runs successfully on different machine with same OS. Now how do i ensure that the app with run successfully in the machine it is deployed in?

like image 328
Varun Achar Avatar asked Nov 10 '11 07:11

Varun Achar


People also ask

How do I fix access denied error in Java?

Right click java folder, click properties. Select the security tab. There, click on "Edit" button, which will pop up PERMISSIONS FOR JAVA window.

What is Java IO FileNotFoundException?

java.io.FileNotFoundException. Signals that an attempt to open the file denoted by a specified pathname has failed. This exception will be thrown by the FileInputStream , FileOutputStream , and RandomAccessFile constructors when a file with the specified pathname does not exist.


Video Answer


4 Answers

On any OS you can have only a certain number of open files/threads at a stretch. You seem to be hitting your OS limit. Try setting file to null inside the loop.

like image 56
isobar Avatar answered Oct 21 '22 04:10

isobar


If I understand your stack trace correctly, the exception is coming when trying to create a new FileWriter. It's impossible to know the reason without investigating a bit further.

  • Return values may tell something. Especially, check what File.delete() returns.
  • Before trying to create new FileWriter, check what File.exists() returns.

If the previous delete() returns true and the exists() right after it also returns true, in a single-threaded program, then it's indeed something weird.

Edit: so it seems that deletion was successful and the file didn't exist after that. That how it's supposed to work, of course, so it's weird why FileWriter throws the exception. One more thought, try checking File.getParentFile().canWrite(). That is, do your permissions to write to the directory somehow disappear.

Edit 2:

Don't get the error on a different machine with the same OS. Now how do i make sure that this error won't come in the app where it'll be deployed?

So far you have one machine that works incorrectly and one that works correctly. Maybe you could try it on even more machines. It's possible that the first machine is somehow broken and that causes errors. It's amazing how often digital computers and their programs (I mean the OS and Java, not necessarily your program) can be just a "little bit broken" so that they work almost perfectly almost all of the time, but fail randomly with some specific hardware & use case - usually under heavy load - similar to how incorrect multi-threaded programs can behave. It doesn't have to be your fault to be your problem :-)

Frankly, the only way to make sure that errors won't come up in machine X is to run the program on machine X. Unusual stuff such as creating and deleting the same file 8000 times in rapid succession is prone to errors, even though it "should" work. Computers, operating systems and APIs are not perfect. The more unusual stuff you do, the more often the imperfections will realize themselves, because unusual usage is generally less thoroughly tested than everyday operations.

like image 29
Joonas Pulakka Avatar answered Oct 21 '22 04:10

Joonas Pulakka


I have had the same issue, a java program (single threaded) that opens, deleted then re-opens the same file continuously.

On some windows systems we get the same issue as reported here, on Linux, Solaris, and various other windows systems it works fine.

Traceing the program with SysInternals Process Monitor (now MS) its clear the delete is done first, at the OS level, and clear the subsequent open fails with PENDING DELETE status.

So there seems to be some slight delay at the OS/NTFS/Disk level before the file is actually deleted, and that seems to be the cause of the random failure in our case.

As a workaround, I changed the .delete() call to instead just write over the top of it new FileWriter(file) and that seems to be working.

The problem did not occur on all systems, one specific model that would always fail, all be it after not fixed number of loops, was a Windows 7 / Dell Lattitude E6420 with WD Smartdrive, whereas my Windows 7 / Dell precision M4600 (with solid state drive) or T3400 with Linux I have never had the issue.

Cheers - Mark

like image 41
Mark O'Donohue Avatar answered Oct 21 '22 04:10

Mark O'Donohue


It may be a long shot, but, can you try to work with a file that is NOT directly sitting on the Desktop. Instead of:

"C:\\Users\\varun.achar\\Desktop\\TODO.txt"

Try:

"C:\\Users\\varun.achar\\SomeOtherDirectory\\TODO.txt"

OS may be killing you here with all the Desktop hooks...

EDIT based on the comments:

  • Are there any scheduled jobs running on the "bad" machine?
  • Instead of debugging the environment, do you have a sys admin to do that?
  • Does this work on a clean Windows install? [95% chance it will]
  • Since the root cause seems to be environment, instead of solving a Windows configuration problem, would you be able to move forward with other tasks, and leave it to someone who keeps the list of discrepancies between the systems?
like image 37
tolitius Avatar answered Oct 21 '22 05:10

tolitius