修改执行Jar文件

发布于 2024-08-25 22:17:26 字数 663 浏览 5 评论 0原文

堆栈溢出的朋友们大家好。我有一个简单的问题,我担心没有简单的解决方案,我需要有关如何继续的建议。我正在开发一个打包为可执行 JAR 的 java 应用程序,但它需要在执行期间修改其一些 JAR 文件内容。在这个阶段,我遇到了一个问题,因为某些操作系统锁定了文件,阻止对其进行写入。

尽管我对于如何实现这一点可以非常灵活,但用户在应用程序退出时看到 jar 文件的更新版本至关重要。干净高效的解决方案显然是可取的,但可移植性是唯一的硬性要求。

以下是我能看到的解决问题的三种方法,请随意评论或建议其他方法。

  1. 告诉 Java 解锁 JAR 文件以进行写入(这似乎不可能,但这将是最简单的解决方案)
  2. 在应用程序启动时将可执行类文件复制到临时文件,使用类加载器加载这些文件并从初始文件中卸载这些文件JAR 文件。(对类加载器没有太多经验,但希望 JVM 足够聪明,能够意识到原始 JAR 不再使用,因此将其解锁)
  3. 将第二个可执行 JAR 文件放入第一个文件中,在启动时提取内部文件jar 到 e 临时文件,使用复制的内部 JAR 调用一个新的 java 进程并将其传递到外部 JAR 的位置,第一个进程退出,第二个进程不受阻碍地修改外部 jar。(这可以工作,但我不确定是否有一个独立于平台的平台)一个java应用程序调用另一个应用程序的方式)

我知道这是一个奇怪的问题,但任何帮助将不胜感激。

Hello Stack Overflow friends. I have a simple problem which i fear doesnt have a simple solution and i need advice as to how to proceed. I am developing a java application packaged as and executable JAR but it requires to modify some of its JAR file contents during execution. At this stage i hit a problem because some OS lock the file preventing writes to it.

It is essential that the user sees an updated version of the jar file by the time the application exits allthough i can be pretty flexible as to how to achieve this. A clean and efficient solution is obviously prefereable but portability is the only hard requirement.

The following are three approaches i can see to solving the problem, feel free to comment on them or suggest others.

  1. Tell Java to unlock the JAR file for writing(this doesnt seem possible but it would be the easyest solution)
  2. Copy the executable class files to a tempory file on application startup, use a class loader to load these files and unload the ones from the initial JAR file.(Not had much experience with the classloaders but hopefully the JVM would then be smart enough to realize that the original JAR is nolonger in use and so unlock it)
  3. Put a Second executable JAR File inside the First, on startup extract the inner jar to e temporaryfile, invoke a new java process using the copyed inner JAR and pass it the location of the Outer JAR, first process exits, second process modifys the Outer jar unincumbered.(This will work but im not sure there is a platform independant way of one java app invoking another)

I know this is a weird question but any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咆哮 2024-09-01 22:17:26

一种选择:

让程序写入 JAR 文件的修改副本

在 JAR 文件中包含第二个 JAR,其中包含一个实用程序,该实用程序在执行时会删除原始 JAR 文件并重命名修改后的副本以与原始副本匹配。

当您的程序退出(并且已进行更改)时,提取此实用程序并在其自己的 JVM 中启动它(使用 Runtime.getRuntime().exec() )。该实用程序将等待锁定解除原始的、未修改的 JAR,然后执行其操作并退出。

对于用户来说,JAR 文件似乎在退出时更新(或者足够接近!)。

One option:

Have the program write a modified copy of the JAR file.

Include in the JAR file a second JAR with a utility that, when executed, deletes the original JAR file and renames the modified copy to match that of the original.

When your program exits (and changes have been made) extract this utility and launch it in its own JVM (using Runtime.getRuntime().exec()). The utility would wait for the lock to go off the original, unmodified JAR and then do its business and exit.

To the user the JAR file would seem to update on exit (or close enough!).

┈┾☆殇 2024-09-01 22:17:26

老实说,我认为你的做法是错误的。这根本不适合部署 java 的默认方式。您修改的文件只需保留到某个地方的 db 或 xml 文件 - 这是唯一明智的方法。

任何事情都只是“逆风而行”——你可能会费一些力气才能让它发挥作用,但它最终会咬住你或客户。

I honestly think your approach is wrong headed. This is simply not a good fit to the default way of deploying java. Your modified files will just have to persist to a db or xml file somewhere - that is the ONLY sensible approach.

Anything is just "fighting against the wind" - you might with some struggling get it to work, but it WILL bite you or customer eventually.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文