title: Load a class from a jar having dependency on another jar
body: <p>My project structure is the following (very simplified of course):
  <a href="https://i.stack.imgur.com/8DABr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8DABr.png" alt="project structure" /></a></p>
  <p>So under <code>lib-ext</code> i download on a daily basis from a Jenkins server 2 jar files 'jar1 and jar2' to be checked by my program, i need one file from 'jar1' lets call it: &quot;Class2Bloaded&quot;.
  The issue is that this file implements an interface that is to be found in 'jar2', lets call this 'Dependency'</p>
  <p>What i would like to do is, from my class under src &quot;ClassThatLoads.java&quot;, load 'Class2Bloaded.class' and tell the class loader to look into 'jar2' to search  for the implementing interface &quot;Dependency.class&quot;</p>
  <p>My code so far (omitting exceptions handling):</p>
  <pre><code>    //Create the URL pointing to Jar1
    private URL getJarUrl(JarFile jarFile)
    {
        return new File(jarFile.getName()).toURI().toURL();
    }
  
    URL jar1Url = getJarUrl(jar1);
    ClassLoader jar1classLoader = new URLClassLoader(new URL[] { jar1Url });
    Class&lt;?&gt; Class2Bloaded = Class.forName(fullClassName, false, jar1classLoader );
  </code></pre>
  <p>So the problem happens within the <code>Class.forName</code> invocation, because the class i want to load implements an interface that is in jar 2.</p>
  <pre><code>Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: com/packagewithinJar2/Dependency
  </code></pre>
  <p>So eventually i have prepared another class loader that points to 'jar2', and i have even got the actual Interface i need:</p>
  <pre><code>URL jar2Url = getJarUrl(jar2);
    ClassLoader jar2classLoader = new URLClassLoader(new URL[] { jar2Url });
    Class&lt;?&gt; Interface2Bloaded = Class.forName(fullClassName, false, jar2classLoader );
  </code></pre>
  <p>Where 'fullClassName' in the second case is the fully qualified name of the interface from which 'Class2Bloaded' depends on.
  Is just that i cant find anything in the javadocs of <code>ClassLoader</code> that allows me to 'inject' an additional class loader for the dependencies.
  I hope my explanation is clear.</p>