title: How to put custom ClassLoader to use?
body: <p>Hello all and thanks for the attention! I have a problem that must both be easy and obvious, yet I am stuck.</p>
  
  <p>I want to deliver dynamically created Java classes to be used by a 3rd party library via a custom ClassLoader.</p>
  
  <p>Now my problem is: How do I set my custom ClassLoader to be used to load this classes when I do not load them directly myself?</p>
  
  <p>I thought when I used my ClassLoader to load a certain class, it became this class's ClassLoader, and all classes loaded from that class would be channeled through my ClassLoader.</p>
  
  <p>I created a custom ClassLoader, following this official tutorial: <a href="http://java.sun.com/developer/onlineTraining/Security/Fundamentals/magercises/ClassLoader/help.html" rel="noreferrer">http://java.sun.com/developer/onlineTraining/Security/Fundamentals/magercises/ClassLoader/help.html</a>.</p>
  
  <pre><code>public class DynamicClassloader extends ClassLoader {
  
      private Map&lt;String, Class&lt;?&gt;&gt; classesMap = new HashMap&lt;String, Class&lt;?&gt;&gt;();
  
      public DynamicClassloader(ClassLoader parent) {
          // Also tried super(parent);
          super(sun.misc.Launcher.getLauncher().getClassLoader());
      }
  
      // Adding dynamically created classes
      public void defineClass(String name, Class&lt;?&gt; clazz) {
          classesMap.put(name, clazz);
      }
  
      @Override
      protected Class&lt;?&gt; findClass(String name) throws ClassNotFoundException {
          // load from parent
          Class&lt;?&gt; result = findLoadedClass(name);
          if (result != null) {
              return result;
          }
          try {
              result = findSystemClass(name);
          } catch (Exception e) {
              // Ignore these
          }
          if (result != null) {
              return result;
          }
          result = classesMap.get(name);
          if (result == null) {
              throw new ClassNotFoundException(name);
          }
          return result;
      }
  }
  </code></pre>
  
  <p>I wanted to use this somewhere else in the code like that:</p>
  
  <pre><code>ClassLoader thisClassLoader = this.getClass().getClassLoader();
  ((DynamicClassloader) thisClassLoader).defineClass(className, dynClass);
  </code></pre>
  
  <p>Now my problem is that when I call <code>findSystemClass(name)</code> of the 3rd party library class, the parent ClassLoader finds this class (because it is on the classpath) and becomes its ClassLoader. And since the parent ClassLoader doesn't know about my custom ClassLoader, it is effectively been put out of use and <code>this.getClass().getClassLoader()</code> cannot be cast to DynamicClassLoader.</p>
  
  <p>Another approach would be to set my ClassLoader to be the system ClassLoader via JVM argument <code>-Djava.system.class.loader=my.DynamicClassloader</code>. But that gives me a StackOverflowError:</p>
  
  <pre><code>    ...
  at de.unisaarland.cs.st.DynamicClassloader.findClass(DynamicClassloader.java:39)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
  at java.lang.ClassLoader.findSystemClass(ClassLoader.java:916)
  at de.unisaarland.cs.st.DynamicClassloader.findClass(DynamicClassloader.java:39)
      ...
  </code></pre>
  
  <p>This must be really easy to do, yet I am now out of ideas... any help is greatly appreciated!</p>