• Post Reply Bookmark Topic Watch Topic
  • New Topic

URLClassLoader locking the jars  RSS feed

 
sreenath reddy
Ranch Hand
Posts: 415
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am using URLClassLoader to load a jar file and then in my code i am not able to delete that jar file as JVM is locking that till it either gets Garbage collected or Program exits

Here is my sample code

File file = new File("sample.jar");
URL url = file.toURL();
URLClassLoader loader = new URLClassLoader(new URL[]{url} , TestLoader.class.getClassLoader());
Class claz = Class.forName("abc" , true, loader);
file.delete(); //But here file is not getting deleted


Can any one let me know a way to overcome this as i need to delete that file immediately ...doesn't mind even classes in the jar are in the JVM
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I testing with following code with java version "1.5.0_06"... It works fine...
Check out cause for the problem would be something else....

import java.io.*;
import java.net.*;

public class FileDeleteTest {
public static void main(String args[]) throws Exception{

File file = new File("c:\\test\\ajp.jar");
URL url = file.toURL();
URLClassLoader loader = new URLClassLoader(new URL[]{url} , FileDeleteTest.class.getClassLoader());
Class claz = Class.forName("FileDeleteTest" , true, loader);
file.delete(); //But here file is not getting deleted


}
}
 
sreenath reddy
Ranch Hand
Posts: 415
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arun

i haven't tested in JDK 1.5 , but how come you are telling that file is deleted , because file.delete will pass with out any exception thrown , but the jar will be still in your hard disk

Let me know if its working for you fine , so that it will confirm that java has done something in 1.5 becuuse this is the behaviour in 1.4
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have tested this code before passing to you. I was able to see jar file getting deleted after running this code. Since I don't have access to lower version of JDK in my system I am not sure about the 1.4, hope some one can share light on this....
Regards
Arun
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!