0

For an ongoing project, we are looking for a possibility to dynamically download and load jar files into a running application. Apart from downloading the files (which is probably fairly straightforward), I am unaware of any solution that would automatically add the jar's to the classpath, and do discovery of the annotations (like CDI beans).

Given such a system, it would be rather handy if the @Inject annotation would not throw a runtime failure of an implementation of a class is not present (because that module-jar was not loaded).

Is there currently any such system? Does spring or OSGi fit this need? Any ideas how close project Jigsaw would come in trying to fulfill this on application level?

Steven De Groote
  • 2,187
  • 5
  • 32
  • 52
  • did you check the http://stackoverflow.com/questions/60764/how-should-i-load-jars-dynamically-at-runtime/60775#60775 – Piotr Reszke Sep 27 '16 at 11:59
  • Looks like a job for reflection. – uoyilmaz Sep 27 '16 at 12:12
  • 2
    Take a look at OSGI mechanism http://stackoverflow.com/questions/15426158/loading-of-osgi-bundle-dynamically-from-a-file-system – Phox Sep 27 '16 at 12:16
  • Jigsaw will (still) not address dynamic loading, just a (far) better module concept. OSGi indeed is the best way to go, also for unloading of jars or other versions. – Joop Eggen Sep 27 '16 at 12:19
  • Steven De Groote, I am from the future, and I have traveled back in time, to write you this comment to tell you this is a bad idea lol – Derrops Oct 04 '16 at 02:39
  • And why would that be @Snickers3192 ? – Steven De Groote Oct 04 '16 at 07:03
  • @mh-dev raised some good points. I think if you should have a lengthy discussion about it with your team. Don't get too attached to your ideas as a developer, you need to be prepared to put them down just like you put your beloved pets down if they become infected with rabies. – Derrops Oct 04 '16 at 23:40

2 Answers2

3

I think you need OSGI, using an OSGI container like Karaf : https://karaf.apache.org

In standard java provide ServiceLoader https://docs.oracle.com/javase/tutorial/ext/basics/spi.html

Mr_Thorynque
  • 1,749
  • 1
  • 20
  • 31
1
  1. I advice you to not follow that path
  2. It should be possible to dynamically load jar files without the usage of OGSI. The keyword are Classloaders especially when used with a proper hierarchy. The following answer should give you an idea: How should I load Jars dynamically at runtime? but keep in mind that this might cause serious security issues
  3. You followed the path at 2. even I advice you not to do it. But now you end up in the scenario that the context of your used framework does not know this classes. You would have this problem with most IOC frameworks. Since they build up the context on startup. There are libraries for this created for development purpose (spring-loaded, spring dev tools, JRebel). If your IOC framework supports it go with it.
  4. Regarding handling not available jars. The best point to do research on this is Spring Boot and its auto configuration mechanism. It checks if certain classes/jars (not sure to be honest) are available and add additional behavior for this cases. But still this is application startup solution and not a runtime IOC solution.
Community
  • 1
  • 1
mh-dev
  • 5,264
  • 4
  • 25
  • 23