Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant understand the working of File object.delete()

Tags:

java

jar

java-5

I am trying to delete a jar file using java 5 (So the Paths API introduced in Java 7 is not an option).

My code:

String sep = File.separator;

File test = new File("."+ sep + "server" + sep + "lib" + sep + "testJar.jar");
if(test.delete())
{
    logger.log(Level.INFO,test.getName() + " deleted.");
}
else
{
    logger.log(Level.INFO,"Delete operation is failed for "+test.getName());
}

When my code gets executed the jar file is deleted but Delete operation is failed for testJar.jar is printed in the logs.Cant understand why..any help is appreciated.

UPDATE: I tried the same code again and and this time it says testJar.jar deleted Now i am confused what is happening

like image 344
amudhan3093 Avatar asked Apr 22 '14 07:04

amudhan3093


1 Answers

I see this problem has magically disappeared, but some general troubleshooting tips:

First of all; try creating and deleting another file and check file permissions/ownership.

Check that another process isn't holding the file:

$ lsof filename

Test the underlying OS call.

File.delete() will delegate to the underlying OS, and will typically end up calling remove (or unlink). If you use OpenJDK, you should be able to browse the source code. If not, see if you can trace what happens "under the covers".

Write a small snippet that simply executes this call and see how the OS behaves when you try and delete this file.

like image 175
andrel Avatar answered Sep 16 '22 14:09

andrel