简体   繁体   English

在Java中运行时下载JAR文件

[英]Downloading JAR files at runtime in Java

I am wondering if it is possible to have a Java desktop application, on startup, look to some URL, to see if it needs an update, and if so download necessary JAR files, and add them to classpath for the running program. 我想知道是否可以在启动时使用Java桌面应用程序,查看某个URL,查看是否需要更新,如果是,则下载必要的JAR文件,并将它们添加到运行程序的类路径中。

If the old jars are there, they shouldn't have been loaded into the classloader yet, at this point should they? 如果旧的罐子在那里,它们不应该已经加载到类加载器中,此时应该是吗? Is it possible to swap them out before they are loaded w/out restarting the application? 是否可以在加载w / out重启应用程序之前将它们换掉?

It should be possible. 它应该是可能的。 The URLClassLoader is a place to start. URLClassLoader是一个开始的地方。

It's possible but prone to error. 它可能但容易出错。

A somewhat cleaner approach is to have a "launcher" application which does not rely on any of the updatable JARs do the above and then launch the actual app. 一种更简洁的方法是拥有一个“启动器”应用程序,它不依赖任何可更新的JAR执行上述操作,然后启动实际应用程序。

You may want to check out this question for more details on this topic. 您可能需要查看此问题以获取有关此主题的更多详细信息。

Java Web Start is probably your best bet. Java Web Start可能是您最好的选择。 The required jar files should be identified in the descriptor allowing your to manage the version control. 应在描述符中标识所需的jar文件,以便管理版本控制。

If you have a dynamic application that needs to discover and download classes dynamically at runtime, then you could look at Dynamic code downloading using RMI . 如果您有一个动态应用程序需要在运行时动态发现和下载类,那么您可以使用RMI查看动态代码下载

Java Web Start is a good choice but has some quirks. Java Web Start是一个不错的选择但有一些怪癖。 Notably: 值得注意的是:

  • Code which is updated must be downloaded before the program starts. 必须在程序启动之前下载更新的代码。 A more modern approach would download updates and apply them at next start. 更现代的方法是下载更新并在下次启动时应用它们。
  • Default is inside a sandbox. 默认位于沙箱中。 If you need to go outside of that you must sign your code, which is rather cumbersome until you automate the deployment process (or use Netbeans which helps quite a bit). 如果你需要在那之外,你必须签署你的代码,这是相当麻烦的,直到你自动化部署过程(或使用Netbeans,这有点帮助)。
  • Problems are not easy to debug - only tool is enabling full trace in the Java Console. 问题不容易调试 - 只有工具在Java控制台中启用完全跟踪。
  • Caching of jars in the client is error prone. 缓存客户端中的jar容易出错。 When you update, be sure that the URL is unique for each deployment component so the cache will not be used. 更新时,请确保每个部署组件的URL都是唯一的,因此不会使用缓存。

But WHEN it works it works pretty well. 但是它工作时它的效果非常好。 It is to my knowledge the easiest way to have a centralized version easily updateable of a given Java application. 据我所知,让集中式版本可以轻松更新给定Java应用程序的最简单方法。

-- -
EDIT: It appears that the "start program and transparently download updates if available" functionality is present in the latest Java 6. I have not tried it yet, but will soon. 编辑:最新的Java 6中似乎存在“启动程序并透明地下载更新(如果可用)”功能。我还没有尝试过,但很快就会尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM