views:

239

answers:

2

Hi,

I'm writing a self-updating application. The first time it runs, it installs a native library on the client's computer, so that they don't need to download it every time. When it detects that the installed library's version is older than the new required minimum, it downloads and installs the new one.

The problem is, the new library isn't actually being used by Java until the user closes and restarts the browser (not the tab, the whole browser). That is something I'm trying to avoid - it's OK if the user needs to refresh the page, or close it and open it again, though.

I have tried copying the installed library to a temp folder every time the applet starts and then always loading that copy, to no avail. Has anyone here ever done this? Any good ideas? The operating system is Linux, JDK 1.5, Firefox.

Thanks!

A: 

Java classes can only be unloaded by unloading the classloader that owns them. Since JNI libraries are dependent on the owning class, you might need to unload the classloader for the class that owns the library.

Try instantiating a URLClassLoader for the class that interfaces with the JNI library, and creating instances of the class in question with reflection. When you want to unload it, create a new classloader, from an updated JAR, which refers to the new version of the library (use a different library filename so you can have both versions loaded simultaneously until the GC takes care of the old one)

bdonlan
+1  A: 

This is exactly the situation described in this bug report: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5075039 (The report says it applies to Windows, but it's actually a Java issue)

There is no way for Java code to unload a native dll until the VM terminates. Once a dll is loaded, it's there for the lifetime of the JVM and the VM will ensure that it is loaded once and only once.

The only way I can see for your scenario to work is if you can somehow do your version check and update before the dll is loaded. Once it's in use, you're stuck with it.

gibbss