mardi 23 décembre 2014

Downloading and adding jar files to classpath at runtime without reflection

We have a standalone java-application. This application has lots of functionality, but most customers does only use a small bit of this functionality. We want to distribute the application with just the "normal" minimum of third-party jar-files. In the eclipse project all external jar-files are in the class path. So no reflection is used to instanciate classes. So when a user are going to use a more advanced function, eg taking webcamera pictures, we will then download all the javacv jar files and then somehow add these to the classpath/classloader before calling our class that takes a picture. Is this possible? I allready have a working solution for a very small module that downloads some jar-files on the fly and adds them in a custom classloader, but I then have to painfully instanciate every class and call every method by reflection. Eg:



URLClassLoader loader = new URLClassLoader(urls,w.getClass().getClassLoader());
Class grabberClass = Class.forName("org.bytedeco.javacv.FrameGrabber",true,loader);
Object grabber = grabberClass.getMethod("createDefault", int.class).invoke(null, 0);
grabberClass.getMethod("start", null).invoke(grabber, null);
//And so on...


This is not what we want. All our source code is just fine, and I don't want to obfuscate it by turning it into reflection-calls. But how may I use my old code and still be able to add the jar-files at runtime?






Aucun commentaire:

Enregistrer un commentaire