5 Replies Latest reply: Aug 29, 2011 8:27 PM by 693129 RSS

    File.mkdirs intermittently failing on Windows 7 64 bit

      I have a workstation with Windows 7 64 bit on it that has two hard drives. The OS is on c: and d: is for my development and source code. When I run the following simple test, baseDir.mkdirs() intermittently fails (once every three or four runs) but only when I create the baseDir on the d: drive and only on the first run through the loop if the javatests directory exists from a prior run. If I create the baseDir directory on the c: drive or use
      Windows Explorer to delete the javatests directory before running the test, it seems to always succeed. The sub1.mkdirs always seems to succeed as well so it is almost as if something is cached and then gets cleared before it reaches that statement.

      Has anyone seen any similar behavior and can offer any help? There are no errors or warnings in the Windows Event logs.
      import java.io.File;
      import java.io.FileWriter;
      public class DirTest {
           public static void main(String [] args) throws Exception {
                File baseDir = new File(System.getProperty("test.dir", "d:/javatests"));
                for (int i = 0; i < 10; i++) {
                     if (!baseDir.mkdirs()) {
                          System.err.println("Failed " + i);
                     File sub1 = new File(baseDir, "test");
                     if (!sub1.mkdirs())  {
                          System.err.println("Failed sub1");
                     File sub2 = new File(baseDir, "test2");
                     if (!sub2.mkdirs()) {
                          System.err.println("Failed sub2");
                     File sec1 = new File(baseDir, "default.sec");
                     File sec2 = new File(sub1, "test.sec");
                     FileWriter fw = new FileWriter(sec1);
                     fw.write("<security>\n " +
                                "<ownerpassword>default</ownerpassword>\n " +
                                "<userpassword strength=\"40\" allowprinting=\"FALSE\">default</userpassword>" +
                     fw = new FileWriter(sec2);
                     fw.write("<security>\n " +
                                "<ownerpassword>test</ownerpassword>\n " +
                                "<userpassword strength=\"128\" allowprinting=\"FALSE\" allowcopy=\"false\">test</userpassword> "+
           private static void deleteDirs(File f) {
                if (f.exists()) {
                     String [] children = f.list();
                     if (children != null)
                          for (int x = 0; x < children.length; x++) {
                               File child = new File(f, children[x]);
                               if (child.isDirectory())
                                    if (!child.delete())
                                         System.err.println("Failed to delete child " + child.getName());
                     if (!f.delete()) {
                          System.err.println("Failed to delete " + f.getName());
        • 1. Re: File.mkdirs intermittently failing on Windows 7 64 bit
          I have a little more information. I was able to get it to fail on the c: drive by opening Windows Explorer and expanding some directories on the c: drive. Windows Explorer seems to affect things. I have not gotten the test to fail if Windows Explorer is closed yet. Seems Explorer must be opened and viewing the drive the test is running on. Maybe Windows sends an event to explorer and my test sometimes beats whatever the event triggers so it fails?!?
          • 2. Re: File.mkdirs intermittently failing on Windows 7 64 bit
            First, the return value of "false" from File.mkdirs does not necessarily mean the operation failed. It just means that that specific call did not create the directory in question. A true test of directory creation failure would be:
            File dir = ...;
            if(!dir.mkdirs() && !dir.isDirectory()) {
              // dir creation failed
            Also, windows tends to lock any file/directory currently "in use". Opening a directory in windows explorer will make it "in use" and stop deletion by another process.
            • 3. Re: File.mkdirs intermittently failing on Windows 7 64 bit
              I have the very same problem with sporadic mkdirs behavior.
              Testing for "!dir.mkdirs() && !dir.isDirectory()" is not my case, there is really no such directory, and mkdirs do fails to create it. But at the next run it most probably succeeds.
              I also have Win7, but mine is x32. BTW near me sits my colleague with the very same laptop, he has no such problems.
              I tried also to run the test in windows safe mode - and it looks like mkdirs behave perfectly!
              And one more point: several times I've observed renameTo sporadic fails.

              Any thoughts?
              • 4. Re: File.mkdirs intermittently failing on Windows 7 64 bit
                The java.nio.file.Files class in jdk7 has a createDirectories method that will throw a useful IOException if it fails.
                • 5. Re: File.mkdirs intermittently failing on Windows 7 64 bit
                  Has anyone found a solution to this problem. I have windows 7 32 bit on a laptop. mkdirs is not working. I am wondering if its a security issue?