Reloading jar files contents dynamically
Asked Answered
A

2

2

I have one jar file in my application's class path. At run time, I add new classes to the jar file and sometimes also modify the fields/methods of the existing classes. Currently I am using URLClassLoader to load the classes dynamically. The new classes added dynamically are loaded correctly and I am able to use them at runtime. But it fails to reload the existing classes that are modified at runtime. I read many articles which states we need to explicitly handle reloading because class once loaded will not be reloaded until all the references to the class are destroyed. Also I tried out sample code that I found but none of them worked.

Can anyone suggest me a proper approach for reloading ? Any sample code for the same will be highly appreciated.

Armipotent answered 31/1, 2013 at 9:25 Comment(1)
post your code. I have done a similar thing already in one of my project. I can help you only when I see the code.Onslaught
D
4

Normally to reload a class you need to unload the entire class loader. i.e. remove all references to all classes loaded for that class loader.

Another option is to use instrumentation to change the byte code of an existing class. This usually comes with limitations and changing fields is something you cannot do. i.e. the objects of that type would have to be translated somehow.

What I normally do is have services which are very quick to start/restart. This way to you easily restart a process which needs updated code ideally by pressing the Run in my IDE. This minimises deployment time as well.

Disengage answered 31/1, 2013 at 9:29 Comment(4)
Can you give me some sample working code for reloading classes. It would be really helpful. I tried out some of the approaches explained in the articles but didn't work.Armipotent
Can you confirm you are willing to unload the ClassLoader and any of the classes loaded? There is no point me showing you a complex example you cannot use.Disengage
Yes, I am willing unload it. Can you provide some sample example which can help ?Armipotent
Create an instance counter just to be sure there is no living instanceTetrahedron
S
1
  1. In principle, a class that has already been loaded cannot be reloaded with the same classloader.
  2. For a new load, it is necessary to create a new classloader and thus load the class.
  3. Using URLClassLoader has one problem and that is that the jar file remains open.
  4. If you have multiple classes loaded from one jar file by different instances of URLClassLoader and you change the jar file at runtime, you will usually get this error: java.util.zip.ZipException: ZipFile invalid LOC header (bad signature). The error may be different.
  5. In order for the above errors not to occur, it is necessary to use the close method on all URLClassLoaders using the given jar file. But this is a solution that actually leads to a restart of the entire application.

A better solution is to modify the URLClassLoader so that the contents of the jar file are loaded into the RAM cache. This no longer affects other URLClassloaders that read data from the same jar file. The jar file can then be freely changed while the application is running. For example, you can use this modification of URLClassLoader for this purpose: in-memory URLClassLoader

Shellbark answered 9/7, 2020 at 8:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.