Dynamically loading jar from arbitrary url

后端 未结 1 1219
野趣味
野趣味 2021-01-24 02:12

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

相关标签:
1条回答
  • 2021-01-24 02:25

    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.

    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();
    
    0 讨论(0)
提交回复
热议问题