Hot swapping the jar files in java

假装没事ソ 提交于 2019-12-19 03:16:54

问题


I have two jar files. One.jar contains main method to start the process and other two.jar contains just class files.

In One.jar I have referenced two.jar in its manifest classpath.

One.jar contains mechanism to dynamically load the classes using Class.forName()

I want to deploy the One.jar only once for lifetime. Will not be making any changes to it.

Changes will be happening only in two.jar

I want to hot swap the two.jar with updated two.jar. I have seen other posts about hot swapping too like OSGi, hotswap for ANT but nothing helps me.

After a little research found out that JRebel does this really better but I really dont know how to integrate it with these jars? I am stuck on this for almost 2 days. Just need few steps of info to get this done. Someone help me please... This is really very important to me... Thanks


回答1:


Hey all I have finally got a solution for this. I really don't know that I will come up with such a easy solution. I took the idea of @Thorbjørn Ravn Andersen. All I did was first get the timestamp of the jar file and then load the jar using URLClassLoader. If the timestamp of the jar file changes (i.e. when I replace the jar with updated jar) then I am discarding old URLClassLoader object using close() and loading the new jar with URLClassLoader again. The only thing I have to check every time is whether the timestamp of jar changes or not. Hope this answer helps other people too. Thank you all for your support.




回答2:


As I am directly related to JRebel (I'm the product manager), I probably can give some pointers for configuration.

To update the classes within a JAR with JRebel, place a dedicated rebel.xml configuration file into that two.jar which will point to the directory where the compiled classes of two.jar are.

     one.jar ---> two.jar
                   `- rebel.xml   // points to two/project/classes

Here's the doc with the examples: http://manuals.zeroturnaround.com/jrebel/standalone/config.html#configuring-jars

Depending on how your project is set up, the IDE plugins would help automating this.

Please note that if your intention is to do this procedure in live system, then JRebel is not a tool for this kind of purpose.



来源:https://stackoverflow.com/questions/20913149/hot-swapping-the-jar-files-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!