I've seen a couple answers to similar questions but unfortunately none have been able to answer mine.
I have a web application that offers a computation platform and in order to extend it I have written an interface, so that the application would check a specific folder for newly added jar files to load classes implementing the interface at runtime.
The problem I'm facing is that even though the class loading is successful am failing to cast the newly created object to my existing interface "ClassCastException"
ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();
URL directory = new URL("file:/Users/Abel/Desktop/Output/strategies/Classes/myJar.jar");
ClassLoader custom = new URLClassLoader(new URL[] {directory}, sysClassLoader);
Class strategyClass = custom.loadClass(className);
Object strategyObject = (Object) strategyClass.newInstance();
Strategy strategyInstance= (Strategy) strategyObject;
Strategy of course is the interface implementes the code (above) and returns the generated Strategy object so I would rather not use reflection in order to invoke the method contained in the loaded class and I apprehend that this would LAO end up with the same problem since the parameters of the method would be considered incompatible too.
From what I've read the class loader considers the existing interface and the one loaded along the class implementing it as two different types hence the exception.
Is there any work around?