[英]How do I update my JRE (or Runtime Image) from my Java application?
I have an application that consists of a JRE (or custom runtime image, as the jlink docs call it), a JAR file, and a method to run the JAR on the JRE (either a bash or batch file or an exe or .app package).我有一个应用程序,它包含一个 JRE(或自定义运行时映像,如 jlink 文档所称)、一个 JAR 文件和一个在 JRE 上运行 JAR 的方法(bash 或批处理文件或 exe 或 .app包裹)。
I want to send out updates for the application (which is working, I can connect to my server and download newJRE.zip and newJAR.jar), but I don't know the best way to replace the currently running JRE with the new JRE.我想发送应用程序的更新(它正在运行,我可以连接到我的服务器并下载 newJRE.zip 和 newJAR.jar),但我不知道用新的 JRE 替换当前运行的 JRE 的最佳方法.
I've tried unzipping the new JRE to a seperate folder, using ProcessBuilder to restart my JAR on the new JRE, then calling System.exit() on the original JRE and from the new JRE deleting the old JRE and copying the new JRE to the original location, and finally restarting my app again on that JRE.我尝试将新 JRE 解压缩到一个单独的文件夹,使用 ProcessBuilder 在新 JRE 上重新启动我的 JAR,然后在原始 JRE 上调用 System.exit() 并从新 JRE 删除旧 JRE 并将新 JRE 复制到原始位置,最后在该 JRE 上再次重新启动我的应用程序。 That didn't work (on Windows, I haven't tried yet on Linux or Mac) because the new, restarted JRE appears to be holding file locks (or handles?) on some of the files in the old JRE, so it can't delete them.这不起作用(在 Windows 上,我还没有在 Linux 或 Mac 上尝试过),因为重新启动的新 JRE 似乎在旧 JRE 中的某些文件上持有文件锁(或句柄?),因此它可以不要删除它们。 I also tried running a batch file that deletes the old JRE and replaces it with the new one, but again it is also unable to delete all the files in the old JRE.我还尝试运行一个批处理文件,删除旧的 JRE 并将其替换为新的,但同样也无法删除旧 JRE 中的所有文件。
Can this be done in Java, or do I have to write my update logic for each platform separately?这可以用 Java 完成,还是我必须分别为每个平台编写更新逻辑? What's the best way to do it?最好的方法是什么?
In theory in can be done but in practice there are hurdles.理论上可以做到,但在实践中存在障碍。
One of them is that on Windows, as long as some executable is executing the file on disk is write-protected.其中之一是在 Windows 上,只要某个可执行文件正在执行,磁盘上的文件就会被写保护。 On Unix this is not the case.在 Unix 上,情况并非如此。 And yet there may be strange effects if you manage to update half the application/JDK before something goes wrong.然而,如果您设法在出现问题之前更新一半的应用程序/JDK,可能会产生奇怪的效果。
I have not really checked but believe all applications that come with autoupdate features circumvent this problem by actually consisting of two applications.我没有真正检查过,但相信所有带有自动更新功能的应用程序实际上都包含两个应用程序来规避这个问题。 One of them is a thin wrapper that checks for updates and gets them installed, then starts the real application.其中之一是一个瘦包装器,它检查更新并安装它们,然后启动真正的应用程序。
If you want this for Java the thin wrapper already exists and is called WebStart.如果你想要这个用于 Java 的瘦包装器已经存在并且被称为 WebStart。 Since Java 9 Oracle no longer supports it, but then there is https://openwebstart.com/由于 Java 9 Oracle 不再支持它,但是有https://openwebstart.com/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.