File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes unloading the jar which is dynamically invoked Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "unloading the jar which is dynamically invoked" Watch "unloading the jar which is dynamically invoked" New topic
Author

unloading the jar which is dynamically invoked

Gurumoorthy Doraiswamy
Greenhorn

Joined: Jan 15, 2013
Posts: 21
Hi,

I have invoked an method is another Jar using the following coderanch URL http://www.coderanch.com/t/529764/java/java/run-jar-java-application

Now i need to copy the new version and replace the old jar.

But my issue is in the old jar i have a timer class which performs a operation periodically. Can any one suggest me how to unload and release all the resource of the old jar and invoke again the new jar.

is there any equivalient like AppDomain.unload() in .NET where the dynamically invoked exe and its resources are released ( i am .NET developer ).

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2364
    
  50
If you use the 2nd technique Rob described ie using URLClassloader then you need to create a new URLClassloader for the new jar. The problem you may have is in releasing the original class loader, to do this you need to release all references to the class loader and all objects loaded by it. This may be trickier than it sounds depending on the complexity of the code.
Gurumoorthy Doraiswamy
Greenhorn

Joined: Jan 15, 2013
Posts: 21
Many thanks for your reply.

Yes i have used the second option URLClassLoader as Rob suggested.

Can you suggest me on this on how to release all the resources loaded by the initial jar.

or is there any other approach for invoking methods in another jar with out the worry of unloading.

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2364
    
  50
Can you suggest me on this on how to release all the resources loaded by the initial jar.

I've already told you how to release the resources from the first jar, you need to release all references to the class loader and all objects loaded by it.

or is there any other approach for invoking methods in another jar with out the worry of unloading.

You don;t necessarily need to unload the first jar because even if the classes in the second jar have exactly the same fully qualified name as those in the first jar the JVM will see them as different classes as they are loaded by different class loaders. But if you don't unload old jars that you no longer need and keep loading new jars you will eventually run into memory problems.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

Don't forget to close() the URLClassLoader (available since Java 7). If you don't the file is (often) still in use until the class loader is garbage collected.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42595
    
  65
That's one of the typical use cases for OSGi (which my be overkill for your purposes, but which solves all aspects of this particular problem for you).


Ping & DNS - my free Android networking tools app
Gurumoorthy Doraiswamy
Greenhorn

Joined: Jan 15, 2013
Posts: 21
Hi,

Many thanks for the replies.

I will investigate the OSGI. Another clarification with using of OSGI,

will this OSGI will also support Android environment? Since our basic code should target both windows and android. The DB layer is written separately.

Sorry if my questions are basic as mentioned i am new to Java.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42595
    
  65
You will be dynamically loading and unloading code on a mobile platform? Wow. I'd be curious about the use case, my guess would be that this is not the best design for a mobile platform.

OSGi is an API, and as such platform-independent, but I doubt there's an implementation for Android. Check out the common implementations listed in http://www.coderanch.com/how-to/java/OSGiLinks; there's one specifically targeted towards mobile platforms.
Gurumoorthy Doraiswamy
Greenhorn

Joined: Jan 15, 2013
Posts: 21
Hi,

Again many thanks for the quick reply.

Let me explain my flow.

Currently we have a jar file which run as windows service through Java service wrapper.

I am trying to have a component like auto updater jar.

If any update is found in the update url then the auto update jar must check for the file version replace the old jar and invoke it.

Hence i thought that the Java service wrapper will start the auto update jar , this will perform the checking whether any new updates are available, if so then it replaces the old jar with new one and invoke it. This is where i find difficulties.

In .NET we have an option of AppDomain.CreateDomain where we can dynamically create an appdomain, load the exe's and invoke method through reflections and unload domain though AppDomain.Unload(). I was trying to apply the same principle to unload a jar.

As said we have our jar file with base which can run in both Android and Windows environment. My intention is to have an automatic update of the jar files.

Please advise me if i am wrong with my approach.



Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42595
    
  65
I don't know if the approach is "wrong" as such -I don't know what the code does, after all- just that it sounds very unusual for a mobile platform, especially considering that it apparently does not have a UI (since it's a Windows service). That's why I'm curious what it does. But yes - I have a hunch that it may not be the best approach for Android.
Gurumoorthy Doraiswamy
Greenhorn

Joined: Jan 15, 2013
Posts: 21
Basically it is a UDP service which communicates with server. If it is alone for windows then is there any approach for automatic updates and executing one jar from another.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: unloading the jar which is dynamically invoked