从任意URL动态加载jar [英] Dynamically loading jar from arbitrary url

查看:153
本文介绍了从任意URL动态加载jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,AWS Lambda增加了对Java的支持。

虽然这是一个好消息,但这对代码的大小(50MB压缩)有严重的限制。虽然这可能适用于其他语言,但Java uberjars可以轻松击败它。

Recently AWS Lambda added support for Java.
While this is great news, this come with a pretty severe limitation to the size of the code (50MB compressed). While this may be fine for other languages, Java uberjars can easily beat that.

所以我一直在想着让一个小型装载机在运行时从其他地方拉入一个更大的罐子。 (如果这是一个好主意暂时搁置一边)。

So I've been toying with the idea of having a small loader that pull in, at runtime, a bigger jar from somewhere else. (set aside if this is a good idea or not for a moment).

从我最初的研究看来,定制类装载机似乎是可行的方法。对于AWS Lambda来说,这可能是不合适的。

From my initial research seems that a Custom Class Loader is the way to go. This is probably a no go for AWS Lambda.

是否还有其他创造性方法可以实现这一目标?

Is there any other creative way this could be achieved?

推荐答案

我认为 ClassLoader ,更准确地说是 URLClassLoader ,是要走的路,我不知道在运行时加载代码的其他解决方案。

I think ClassLoader, and more precisely URLClassLoader, is the way to go, and I don't know of any other solution to load code at runtime.

类加载器甚至不必是自定义的。它仅适用于几行代码,如此帖。

The class loader does not even have to be custom. It works with just a few lines of code, as demonstrated in this post.

如果你要加载的jar文件满足你的应用程序的特定服务,还要考虑方便的 ServiceLoader 。它的工作原理相同(事实上,你可以直接传递一个 ClassLoader ),但是使它从动态加载的库中实例化对象变得透明。否则,您将不得不使用类似的东西弄脏你的手:

If the jar files you will load fulfill a particular service for your application, also consider the handy ServiceLoader. It works on the same principle (in fact, you can pass it directly a ClassLoader), but makes it transparent to instantiate objects from the dynamically loaded library. Otherwise, you would have to get your hands a bit dirty, using something like:

Object main = loader.loadClass("Main", true).newInstance();

这篇关于从任意URL动态加载jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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