[問題] URLClassLoader 釋放 .jar檔 已解決

作者: libertyleave (SSLin)   2018-03-26 10:51:48
環境 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 沒有
不知道如何解決 請問版上的各位大大有好的辦法嗎?
作者: NullLife (廢材大叔有點累)   2018-03-27 09:00:00
一定要1.2這麼悲劇?
作者: libertyleave (SSLin)   2018-03-27 09:56:00
就是這麼悲劇

Links booklink

Contact Us: admin [ a t ] ucptt.com