title: ClassNotFoundException while loading a class from file with URLClassLoader
body: <p>I am using the following code to create an instance of <code>OdbcIniTarget</code> class which implements the <code>Target</code> interface. The <code>OdbcIniTarget</code> class already exists in the application but is located in a different package and now should be loaded from the <em>target</em> directory which is not in the class path.</p>
  
  <pre><code>...
  // directory below is resolved to /D:/Workspace/&lt;myproject&gt;/targets/
  private File targetsLocation = new File("targets");
  ...
  private void loadTargets() {            
  
      URL url = null;
      try {
          url = targetsLocation.toURI().toURL();            
      } catch (MalformedURLException e) {
  
      }
  
      URL[] urls = new URL[]{ url };        
      URLClassLoader classLoader = new URLClassLoader(urls);
  
      try {
  
       Target target =
       (Target)classLoader.
       loadClass("ch.blah.targets.nope.OdbcIniTarget").
       newInstance(); // &lt;--- this fails hard  
  
      } catch (...) {
       ...
      }            
  
      try {
          classLoader.close();
      } catch (IOException e) {
  
      }
  }
  </code></pre>
  
  <p>I am getting the following exception.</p>
  
  <pre><code>java.lang.ClassNotFoundException: ch.blah.targets.nope.OdbcIniTarget
      at java.net.URLClassLoader$1.run(Unknown Source)
      at java.net.URLClassLoader$1.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at ch.blah.targets.TargetFactory.loadTargets(TargetFactory.java:74)
      at ch.blah.targets.TargetFactory.&lt;init&gt;(TargetFactory.java:41)
      at ch.blah.SomeOfMyClasses.&lt;init&gt;(SomeOfMyClasses.java:34)
      at ch.blah.TheMainClass.main(TheMainClass.java:38)
  </code></pre>
  
  <p>Does someone have an idea why the class is not found?</p>
  
  <p>The class which is loaded looks as follows.</p>
  
  <pre><code>package ch.blah.targets.nope;
  
  import org.apache.log4j.Logger;
  
  import ch.blah.utils.Utils;
  
  public class OdbcIniTarget extends Target {
  
      private static final Logger log = Logger.getLogger(OdbcIniTarget.class);    
  
      @Override
      public boolean someMethod1() {
          return false;
      }
  
      @Override
      public boolean someMethod2() {
          return false;
      }
  
      @Override
      public boolean someMethod3() {
          return false;
      }       
  
      @Override
      public void someMethod4() {
          log.debug("Blah.");
          Utils.getInstance().marshalClass(SomeClass.class);
          log.debug("Finished.");
      }
  }
  </code></pre>
  
  <p>The directory structure for <em>targets</em> directory is as follows.</p>
  
  <pre><code>D:\Workspace\&lt;myproject&gt;\targets&gt;dir
   Volume in drive D is DATA
   Volume Serial Number is 021C-EC9B
  
   Directory of D:\Workspace\&lt;myproject&gt;\targets
  
  10.07.2014  21:20    &lt;DIR&gt;          .
  10.07.2014  21:20    &lt;DIR&gt;          ..
  10.07.2014  21:31             1'278 OdbcIniTarget.java
  10.07.2014  20:23             3'761 OdbcIniTargetConfiguration.java
                 2 File(s)          5'039 bytes
                 2 Dir(s)   7'328'571'392 bytes free
  </code></pre>