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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With