Hi I am creating a plugin which requires loading jars dynamically and accessing the classes and methods of these jars. I tried using URLClassLoader and am able to load the classes as shown below
URL myJarFile = new URL("jar","","file:"+jarPath);
URLClassLoader sysLoader =(URLClassLoader)ClassLoader.getSystemClassLoader();
Class sysClass = URLClassLoader.class;
Method sysMethod = sysClass.getDeclaredMethod("addURL", new Class[]{URL.class});
sysMethod.setAccessible(true);
sysMethod.invoke(sysLoader, new Object[]{myJarFile});
But the issue with this is that we have to load classes into classLoader by specifying their name individually. What I want is to load all the classes from all jars in class-path and access them any point of time.
Is it possible with URLClassLoader? If not what are the other options? How much helpful is OSGI in achieving this?
Thanks in advance!
You need to load your jar first and then load the required class from there.
Then you can get a list of all the classes in the jar file first:
Once you have list of your classes do the following:
Apache Felix File Install may be exactly what you want. It will monitor a nominated directory and dynamically load any bundles in it. Only packages exported by the bundles will be available to other bundles on the classpath, but all classes will be loaded.