I have to use a 3rd party platform, but the platform has an older version of the jar libjar-1.0.0.jar
that cannot be replaced. The platform let me run my own (flat file) packages on top of it. I put the new version of libjar-2.0.0.jar
under my package /packages/package-name/external-jar. When I used URLClassLoader
to load libjar-2.0.0.jar
and then printing out all declaredmethods, I was able to see the method that is in 2.0.0 jar. However, when I invoke, I always get NoSuchMethodException
. When I print out newobj.class.getProtectionDomain().getCodeSource().getLocation().toString()
, it always shows libjar-1.0.0.jar
. Could anyone help explaining what I did wrong and what I need to do to force using the classes in a particular jar during runtime?
Here is a snapshot of my code
File f = new File(path);
URL[] urls = new URL[1];
urls[0] = f.toURI().toURL();
ClassLoader cl = new URLClassLoader(urls);
Class<?> utilsClass = cl.loadClass("com.myclass");
Constructor<?> cons = utilsClass.getConstructor(First.class, Second.class);
Object utils = cons.newInstance(firstObj, new Second());
if (utilsClass.getProtectionDomain() != null) {
LOGGER.info(utilsClass.getProtectionDomain().getCodeSource().getLocation().toString());
}
// this print out --- 1.0.0.jar instead of 2.0.0.jar
for (Method m : utilsClass.getDeclaredMethods()) {
LOGGER.info("methods: " + m.getName());
}
// method shows the "methodILookFor"
Method m = utilsClass.getDeclaredMethod("methodILookFor", Target.class, String[].class, Object.class);
// always throws NoSuchMethodException
m.invoke(utils, target, string, obj);