12

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

Joel
  • 4,732
  • 9
  • 39
  • 54
amudhan3093
  • 740
  • 9
  • 17
  • 3
    It's likely the Jar file is open by some one, like the JVM for example. You could try using `deleteOnExit` which will try and delete the file when the JVM terminates normally, but it's still likely not to work. I believe Java 7 corrects this issue by having the `Classloader` close the Jar once it's finished reading it... – MadProgrammer Apr 22 '14 at 07:06
  • 11
    Use `Files.delete(Path)` - that throws an exception rather than returning a `boolean`. To get a `Path` either use `File.toPath` for `Paths.get`. That should give you a descriptive error. – Boris the Spider Apr 22 '14 at 07:07
  • @BoristheSpider Thanks for the tip.The nio was introduced in java 7 while i am restricted to work with java5..any suggestions for that? – amudhan3093 Apr 28 '14 at 05:35
  • Does `File.deleteOnExit` work for your application? I tried a test with a jar that is in use and it won't delete it but instead prints failed. However you say that the jar gets deleted but yet it's printing the log that says it failed. Can you also try printing to the console? – Alvin Bunk Apr 28 '14 at 06:19
  • take a look at http://commons.apache.org/proper/commons-io/javadocs/api-2.4/ maybe it does a better delete. – Chris May 13 '14 at 13:42
  • What platform are you on? Java's File.delete() method just calls the underlying operating system and there are signficant differences between Windows and Unix (Linux, Mac OS X) for example. You might even see different behaviour depending on the file system (network volume vs. local drive etc.) And on many platforms if the file is open that will lock it and prevent it being deleted. – Rolf May 29 '14 at 22:48
  • 7
    Nobody seems to be reading the actual question. The OP says the file *is* deleted, but `delete()` is returning false. – user207421 Jun 02 '14 at 01:27
  • I've just failed to recreate the problem. I'd second Rolf's suggestion of OS / network issues, assuming that nothing else is removing the file / causing it to not exist in the first place. – Dan Jun 18 '14 at 09:02
  • 1
    Even i failed to recreate the problem.I think this question should be closed – amudhan3093 Jun 18 '14 at 09:27
  • You have failed to recreate a problem - means that you description of issue is incomplete, not all conditions are described. – win_wave Jun 21 '14 at 09:54
  • @win_wave Fair enough.I dont know what i conditions i failed to notice,so i flagged it to be closed – amudhan3093 Jun 21 '14 at 10:46
  • Does anything change if you remove `"."+ sep +`? Even though I don't expect it to, it's unnecessary. Please also not that the title is misleading; you're actually confused by the behavior of `File.delete()`. – Michel Jung Jun 28 '14 at 12:20
  • Are you sure the file has really been deleted by your code? How can you tell? Isn't it possible that the file did not exist at all? Or that it was deleted by some other application? This would explain both the observed behavior and the unability to reproduce again. – Marwin Jun 29 '14 at 14:41
  • @Marwin Yes i am sure the jars were deleted and the files existed. – amudhan3093 Jun 29 '14 at 16:35

2 Answers2

1

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.

andrel
  • 1,144
  • 11
  • 25
0

File delete fails if the file that you are trying to delete does not exists of is blocked. Also fails for security reasons (insufficient privileges to execute on local system)

The tipical case is that is blocked for example if the jar is used by a running application, or opened exclusively, or analized by a tool (like an antivirus),...

Dubas
  • 2,855
  • 1
  • 25
  • 37