title: Oracle Database Deleting Files Using Java
body: <p>Sometime this weekend one of our databases (11.1.0.7) began having problems accessing certain folders on a portion of the network through Java. I have reproduced the problem using a small portion of java that does nothing but create a file object and attempt to delete the file. From the
  database this works for deleting local files and deleting network files on our \zion\dp\ share, but not from our \zion\it\ share.  </p>
  
  <p>We have another database that runs under the same domain users account that has no problems deleting files from this location. Also logged in as the same domain user on the server having problems I can run the java outside of Oracle and have no problems deleting
  files. The domain user has full control over the folder and logged in as the user I can create, modify, and delete files.</p>
  
  <p>If I haven't granted my oracle database user the appropriate dbms_java permissions I get the appropriate java.security.AccessControlException error.  After I grant the permission the java runs to completion, the delete command returns false (nothing deleted) and the file is not deleted.</p>
  
  <p>I opened a case with Oracle, but it looks like they aren't going to help any more because it involves file commands being run from the java layer even though it is only reproducible from the Oracle environment.</p>
  
  <p>Test Code:</p>
  
  <pre><code>import java.io.*;               
  import java.sql.*;                 
  import java.util.*;             
  
  public class Ajclass
  {
     public static void ajprocedure(String pdfFileName) throws Exception
     {
        boolean result;
        try {
           System.out.println("Start!");
  
           File file = new File(pdfFileName);
           //result = file.delete();
           result = file.exists();
           if (result == true) 
             System.out.println("xxFile deleted.");
           else
             System.out.println("xxFile NOT deleted!");
           System.out.println("End!");
        } catch ( Exception e ) {
           throw(e);
        }         
     }
  }
  </code></pre>
  
  <p>Other code I recently found to be failing only against this share and only when run from inside this database:</p>
  
  <pre><code>import java.io.*;
  import java.sql.*;
  
  public class DirectoryListing
  {
  public static void getList(String directory) throws SQLException
     {
        File path = new File( directory );
        String[] list = path.list();
        String element;
        int CurrentFile;
  
        for(CurrentFile = 0; CurrentFile &lt; list.length; CurrentFile++)
        {
          element = list[CurrentFile];
          #sql { INSERT INTO DIRECTORYLISTING (FILENAME) VALUES (:element) };
        }
     }
  }
  </code></pre>