File Delete issue

I cant seem to get a file deleted no matter how I try using the JAVA File class.  I have made sure that all streams and other potential programtic references to the file have been closed and nulified.  I can remove these files manually using windows explorer so I would think there are no outstanding file locks or sharing violations etc.  I have tried all manner of things and the code snippet below is what I have implemented as of this time.
File myFile = null;
File myDirFile = null;
String[] filesToRemove;
 
myDirFile = new File(this.sBasePath);
filesToRemove = myDirFile.list();
for (int i=0; i < filesToRemove.length; i++) {
     if (filesToRemove[i].toUpperCase().contains(this.sName.toUpperCase()) == true) {
          myFile = new File(myDirFile + this.sFileSeparator,  filesToRemove[i]);
          if (myFile.delete() == true)
              System.out.println("Deleted file: " + filesToRemove[i]);
           else
               System.out.println("Failed to delete file: " + filesToRemove[i]);
       }
}

Open in new window

FlyByNtAzAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
What's your debug output saying?

You can simplify your code (and make it less error prone) by doing

File[] filesToRemove = myDirFile.listFiles();

then you don't need to construct files from path fragments
FlyByNtAzAuthor Commented:
Below is the console output from the code snippet in the problem statement:

Failed to delete file: SMMA_LO_00001.jar
Failed to delete file: smma_lo_00001.zip
CEHJCommented:
Can you change

if (myFile.delete() == true)

into the following and then report back?
if (myFile.exists()) {
	System.out.printf("File %s exists, so attempting to delete it\", myFile);
}
else {
	System.err.printf("File %s does NOT exist, so skipping it\", myFile);
	continue;
}
if (myFile.delete() == true)

Open in new window

Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

CEHJCommented:
Oops, sorry
if (myFile.exists()) {
	System.out.printf("File %s exists, so attempting to delete it\n", myFile);
}
else {
	System.err.printf("File %s does NOT exist, so skipping it\n", myFile);
	continue;
}
if (myFile.delete() == true)

Open in new window

FlyByNtAzAuthor Commented:
Modified as per your two suggestions w/o any difference in behavior.

The following is the console output:
File: C:\DOCUME~1\Delema\LOCALS~1\Temp\math\SMMA_LO_00001.jar exists, so attempting to delete it
Failed to delete file: C:\DOCUME~1\Delema\LOCALS~1\Temp\math\SMMA_LO_00001.jar
File: C:\DOCUME~1\Delema\LOCALS~1\Temp\math\smma_lo_00001.zip exists, so attempting to delete it
Failed to delete file: C:\DOCUME~1\Delema\LOCALS~1\Temp\math\smma_lo_00001.zip

File myDirFile = null;
File[] filesToRemove;
 
myDirFile = new File(this.sBasePath);
filesToRemove = myDirFile.listFiles();
for (int i=0; i < filesToRemove.length; i++) {
    if (filesToRemove[i].getName().toUpperCase().contains(this.sName.toUpperCase()) == true) {
        if (filesToRemove[i].exists()) {
            System.out.printf("File: %s exists, so attempting to delete it\n", filesToRemove[i]);
        }
        else {
            System.out.printf("File: %s does NOT exist, so skipping it\n", filesToRemove[i]);
            continue;
        }
        if (filesToRemove[i].delete() == true)
          System.out.println("Deleted file: " + filesToRemove[i]);
       else
           System.out.println("Failed to delete file: " + filesToRemove[i]);
    }
}

Open in new window

CEHJCommented:
Are those zip/jar files in any way involved in your classpath?
FlyByNtAzAuthor Commented:
No, they are extracted as resources from a JAR file that is used as a container much like an image file that you would find in a JAR file.
CEHJCommented:
Try running the 'handle' utility from sysinternals on those files to see if anything has a lock on them

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FlyByNtAzAuthor Commented:
As you expected there seem to be dangling file handle references hanging around as seen by the following output from the handle utility you mentioned:

------------------------------------------------------------------------------
java.exe pid: 5956 NCSP\Delema
    C: File  (RW-)   C:\MATH
--->>>  3ACC: File  (RW-)   C:\DOCUME~1\Delema\LOCALS~1\Temp\math\SMMA_LO_00001.jar
--->>> 3AE4: File  (RW-)   C:\Documents and Settings\Delema\Local Settings\Temp\math\smma_lo_00001.zip
 3AEC: File  (RW-)   C:\MATH\JARS\Deploy\WindowsRTE.jar
 3AF4: File  (RW-)   C:\MATH\JARS\Deploy\SMMA_LO_00004.jar
 3AFC: File  (RW-)   C:\MATH\JARS\Deploy\SMMA_LO_00003.jar
 3B04: File  (RW-)   C:\MATH\JARS\Deploy\SMMA_LO_00002.jar
 3B0C: File  (RW-)   C:\MATH\JARS\Deploy\SMMA_LO_00001.jar
 3B14: File  (RW-)   C:\MATH\JARS\Deploy\ProgressReport.jar
 3B40: File  (RW-)   C:\DOCUME~1\Delema\LOCALS~1\Temp\Manifest.jar
 3B48: File  (RW-)   C:\MATH\JARS\Deploy\Manifest.jar
 3B50: File  (RW-)   C:\MATH\JARS\Deploy\MacRTE.jar
 3B58: File  (RW-)   C:\MATH\JARS\Deploy\Common.jar
 3B70: File  (RW-)   C:\Program Files\Java\jdk1.5.0_14\jre\lib\ext\sunpkcs11.jar
 3B78: File  (RW-)   C:\Program Files\Java\jdk1.5.0_14\jre\lib\ext\sunjce_provider.jar
 3B84: File  (RW-)   C:\Program Files\Java\jdk1.5.0_14\jre\lib\ext\localedata.jar
 3B8C: File  (RW-)   C:\Program Files\Java\jdk1.5.0_14\jre\lib\ext\dnsns.jar
 3E4C: File  (RW-)   C:\Program Files\Java\jdk1.5.0_14\jre\lib\charsets.jar
 3E54: File  (RW-)   C:\Program Files\Java\jdk1.5.0_14\jre\lib\jce.jar
 3E5C: File  (RW-)   C:\Program Files\Java\jdk1.5.0_14\jre\lib\jsse.jar
 3E64: File  (RW-)   C:\Program Files\Java\jdk1.5.0_14\jre\lib\rt.jar
 3E88: Section       \BaseNamedObjects\hsperfdata_Delema_5956
 3E8C: File  (R-D)   C:\DOCUME~1\Delema\LOCALS~1\Temp\hsperfdata_Delema\5956
 3FD0: Section       \BaseNamedObjects\ShimSharedMemory

C:\Documents and Settings\Delema\Desktop>
CEHJCommented:
Unfortunately i can't reproduce this at the moment. can you not wait until a bit later to delete them?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.