title: classloader: how to load different version of jar
body: <p>I have to use a 3rd party platform, but the platform has an older version of the jar <code>libjar-1.0.0.jar</code> that cannot be replaced. The platform let me run my own (flat file) packages on top of it. I put the new version of <code>libjar-2.0.0.jar</code> under my package /packages/package-name/external-jar. When I used <code>URLClassLoader</code> to load <code>libjar-2.0.0.jar</code> 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 <code>NoSuchMethodException</code>. When I print out <code>newobj.class.getProtectionDomain().getCodeSource().getLocation().toString()</code> , it always shows <code>libjar-1.0.0.jar</code> . 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? </p>
  
  <p>Here is a snapshot of my code</p>
  
  <pre><code>File f = new File(path);
  
  URL[] urls = new URL[1];
  urls[0] = f.toURI().toURL();
  ClassLoader cl = new URLClassLoader(urls);
  
  Class&lt;?&gt; utilsClass = cl.loadClass("com.myclass");
  Constructor&lt;?&gt; 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);
  </code></pre>