在运行时更新 JAR [英] Updating a JAR whilst running

查看:37
本文介绍了在运行时更新 JAR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设 jar 在 JVM 中运行,是否可以卸载当前运行的 Jar 并将其从系统中删除.下载新版本并将其重命名为与上一个 Jar 相同的名称,然后初始化新 Jar,从而在 JVM 中创建 Jar 的无缝更新.甚至可以指示 JVM 执行此操作吗?是否可以在运行时更新 Jar?

Given a jar runs within a JVM would it be possible to unload the current running Jar and remove it from the system. Download a new version and rename it with the same name of the last Jar and then initialise the new Jar, creating a seamless update of the Jar within the JVM. Is it even possible to instruct the JVM to perform this action? Is it even possible to update a Jar whilst its running?

推荐答案

这是我以前见过很多次的事情(我自己也做过).我写了一些可能出现的问题/解决方案.

This is something that I've seen done many times before (and also done myself). I composed some points of the problems/solutions that might arise.

  • 如果您覆盖了稍后将使用的 JAR 文件,JVM 将因转储而崩溃.
    • 稍后我的意思是类的加载非常缓慢,有些可能只会在程序生命周期的后期加载
    • JVM 有一个 JAR 文件的打开句柄,当 JAR 和指针错误时,lib 将失败
    • 可以通过预加载 JAR 文件中的所有类和资源来降低概率
    • 如果您有自定义类加载器,则可以自己关闭句柄.
    • 一个自定义类加载器,它将为每个 JAR 创建一个类加载器并管理版本控制
    • 了解您的应用程序如何使用类加载器以及它如何作用于新的 JAR(例如,检查 Tomcat 在您覆盖 WAR 存档时做了什么)

    总而言之,当您想要实现 JAR 重新加载时,您可能会遇到很多问题.幸运的是,有一些方法可以将风险降到最低.最安全的方法是做类似的事情以获得相同的效果.最干净的是自定义类加载器和 JAR 文件版本控制.

    All in all there are many issues that you might run into when you want to achieve JAR reloading. Luckily there are ways how to minimise the risks. The safest way is to do something similar to get the same effect. Cleanest is custom classloaders and JAR file versioning.

    这篇关于在运行时更新 JAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆