環境 win7 版本: jdk1.2
寫了一段 function 載入 .jar 檔執行 然後希望 function 結束後刪除 .jar 檔
但是不知道為何 .jar檔無法被刪除
code 大致如下
URL url = new URL("file:/C:/java/A.jar");
URLClassLoader ul = new URLClassLoader(new URL[] { url });
Class c = ul.loadClass(name);
Method m = c.getMethod("main", new Class[] { args.getClass() });
m.setAccessible(true);
int mods = m.getModifiers();
if ( !Modifier.isStatic(mods) || !Modifier.isPublic(mods))
{
throw new NoSuchMethodException("main");
}
try
{
String result = m.invoke(null, new Object[] { args }).toString();
if(result.indexOf("0")!=-1)
System.out.println("Close");
File f=new File("C:/java/A.jar");
if(f.exists()){
System.out.println("Delete Old jar");
f.delete();
}
}
catch (Exception e)
{
System.out.println("Ex: "+e.getMessage());
}
delete 部分也沒跳 exception, 但是 A.jar還在 沒有被刪除
手動刪除的話 windows 會跳出訊息說正被 java 使用 無法被刪除
google 之後找不太到解法 有看到說可以呼叫 URLClassLoader.close()
但是 java 1.2 沒有
不知道如何解決 請問版上的各位大大有好的辦法嗎?