1

我有一个应用程序,它由 JRE(或自定义运行时映像,如 jlink 文档所称)、JAR 文件和在 JRE 上运行 JAR 的方法(bash 或批处理文件或 exe 或 .app包裹)。

我想发送应用程序的更新(它正在工作,我可以连接到我的服务器并下载 newJRE.zip 和 newJAR.jar),但我不知道用新 JRE 替换当前运行的 JRE 的最佳方法.

我尝试将新 JRE 解压缩到单独的文件夹,使用 ProcessBuilder 在新 JRE 上重新启动我的 JAR,然后在原始 JRE 上调用 System.exit() 并从新 JRE 中删除旧 JRE 并将新 JRE 复制到原始位置,最后在该 JRE 上再次重新启动我的应用程序。这不起作用(在 Windows 上,我还没有在 Linux 或 Mac 上尝试过),因为重新启动的新 JRE 似乎在旧 JRE 中的某些文件上持有文件锁(或句柄?),所以它可以不要删除它们。我还尝试运行一个批处理文件,删除旧 JRE 并将其替换为新 JRE,但它也无法删除旧 JRE 中的所有文件。

这可以用 Java 完成,还是我必须分别为每个平台编写更新逻辑?最好的方法是什么?

4

1 回答 1

0

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. On Unix this is not the case. And yet there may be strange effects if you manage to update half the application/JDK before something goes wrong.

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. Since Java 9 Oracle no longer supports it, but then there is https://openwebstart.com/

于 2021-07-23T06:07:00.040 回答