title: Reload used classes at runtime Java
body: <p>I'm working on a program that watches a directory and runs all tests in the directory when it sees changes in the directory.</p>
  
  <p>This requires the program to dynamically load the classes, instead of getting the cached copies.</p>
  
  <p>I can dynamically load the test classes. Changes to the tests get detected and used at runtime. However, this isn't the case for the classes tested by the tests.</p>
  
  <p>My code for dynamically loading the classes and returning a list of test classes:</p>
  
  <pre><code>List&lt;Class&lt;?&gt;&gt; classes = new ArrayList&lt;Class&lt;?&gt;&gt;();
      for (File file : classFiles) {
          String fullName = file.getPath();
          String name = fullName.substring(fullName.indexOf("bin")+4)
                  .replace('/', '.')
                  .replace('\\', '.'); 
          name = name.substring(0, name.length() - 6);
  
              tempClass = new DynamicClassLoader(Thread.currentThread().getContextClassLoader()).findClass(name)          } catch (ClassNotFoundException e1) {
              // TODO Decide how to handle exception
              e1.printStackTrace();
          }
  
          boolean cHasTestMethods = false;
          for(Method method: tempClass.getMethods()){
              if(method.isAnnotationPresent(Test.class)){
                  cHasTestMethods = true;
                  break;
              }
          }
          if (!Modifier.isAbstract(cachedClass.getModifiers()) &amp;&amp; cHasTestMethods) {
              classes.add(tempClass);
          }
      }
      return classes;
  </code></pre>
  
  <p>with DynamicClassLoader being as the Reloader described here <a href="https://stackoverflow.com/questions/3971534/how-to-force-java-to-reload-class-upon-instantiation">How to force Java to reload class upon instantiation?</a></p>
  
  <p>Any idea how to fix it? I thought all classes would be dynamically loaded. Note however that I don't overwrite loadclass in my DynamicClassLoader because if I do my test classes give init</p>
  
  <p>EDIT:
  This doesn't work, the class gets loaded but the tests in it aren't detected...</p>
  
  <pre><code>List&lt;Request&gt; requests = new ArrayList&lt;Request&gt;();
      for (File file : classFiles) {
          String fullName = file.getPath();
          String name = fullName.substring(fullName.indexOf("bin")+4)
                  .replace('/', '.')
                  .replace('\\', '.'); 
          name = name.substring(0, name.length() - 6);
          Class&lt;?&gt; cachedClass = null;
          Class&lt;?&gt; dynamicClass = null;
          try {
              cachedClass = Class.forName(name);
  
  
              URL[] urls={ cachedClass.getProtectionDomain().getCodeSource().getLocation() };
              ClassLoader delegateParent = cachedClass .getClassLoader().getParent();
              URLClassLoader cl = new URLClassLoader(urls, delegateParent) ;
              dynamicClass = cl.loadClass(name);
              System.out.println(dynamicClass);
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  </code></pre>
  
  <p>Edit edit: i detect the test methods like this:</p>
  
  <pre><code>            for(Method method: dynamicClass.getMethods()){
              if(method.isAnnotationPresent(Test.class)){
                  requests.add(Request.method(dynamicClass, method.getName()));
              }
          }
  </code></pre>