Reloading jar files contents dynamically

后端 未结 2 1364
礼貌的吻别
礼貌的吻别 2021-01-23 06:31

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. Current

2条回答
  •  野的像风
    2021-01-23 06:34

    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

提交回复
热议问题