title: ClassNotFoundException using ClassLoader in a FileWalker
body: <p>I have a program where the User selects a Directory to begin a FileWalk from; the FileWalker visits each file in the directory structure loading <code>.class</code> files with a <code>CLassLoader</code> so I can use <code>Reflection</code> on them to display information about that class in a GUI.</p>
  
  <p>If the user selects the folder FileReader which has this strucuture:</p>
  
  <pre><code>D:\Users\Ste\Documents\Eclipse Workspace\Project Tests\File Reader
      │
      ├───+bin
      │   │   ReadFile.class   
      |
      │
      └───+src
          │   ReadFile.java
  </code></pre>
  
  <p>The <code>ClassLoader</code> has no problem loading <code>ReadFile.class</code></p>
  
  <p>However if I select the same directory with an added class in the package Test:</p>
  
  <pre><code>D:\Users\Ste\Documents\Eclipse Workspace\Project Tests\File Reader
      │
      ├───+bin
      │   │   ReadFile.class
      │   │
      │   └───+Test
      │           TestClass.class
      │
      └───+src
          │   ReadFile.java
          │
          └───+Test
                  TestClass.java
  </code></pre>
  
  <p>Eclipse throws a <code>java.lang.ClassNotFoundException: Test.TestClass</code>.</p>
  
  <p>So why am I getting a <code>java.lang.ClassNotFoundException</code>? Below is my code with some <code>System.out.println()</code> to show some values as it goes through. (The directory structure is the same as the strucuture above)</p>
  
  <pre><code>public class ReflectOnClasses extends SimpleFileVisitor&lt;Path&gt; {
  
      //Starts the file walk from a starting directory
      public static void startFileWalk(String directory){
  
          //The startingDir is the directory the user selects on the Main UI Part
          Path startingDir = FileSystems.getDefault().getPath(directory, "");
          System.out.println("Staring Directory the user selected: " + startingDir);
          System.out.println("--------------------------------------------------------------------------------------------------------");
          //Create an instance of my FileVisitor
          ReflectOnClasses reflectOnClasses = new ReflectOnClasses();
  
          try {
              //Walk the files from my startingDir using reflectOnClasses
              Files.walkFileTree(startingDir, reflectOnClasses);
          } catch (IOException e) {
              // TODO Auto-generated catch block
              System.out.println("An Error Occured");
              e.printStackTrace();
          }
      }
  
      //Visit files and do something with them
      @Override
      public FileVisitResult visitFile(Path filesPath, BasicFileAttributes attr) {
  
          //If we find a .class file get it and reflect upon it
          if(filesPath.toString().endsWith(".class")){
  
              //Create a File object of the files directory
              File parentDir = new File(filesPath.getParent().toString());
              System.out.println("Parent Directory: " + filesPath.getParent().toString());
  
              try{
  
                  //Convert parentDir to URL
                  URL url = parentDir.toURL();
                  URL[] urls = new URL[]{url};
  
                  //Create a new class loader with the directory
                  ClassLoader cl = new URLClassLoader(urls);
  
                  /*
                   * To load a class we need the class name and the package it belongs too in the format
                   * "example.package.name\FooBar". We can easily get the class name by getting the name
                   * of filesPath and removing the .class on the end
                   */
                  String className = filesPath.getFileName().toString().replace(".class", "");
                  System.out.println("The className: " + className);
  
                  //classesPackage + className
                  String classToLoad = getPackageName(filesPath) + className;
                  System.out.println("The classToLoad: " + classToLoad);
  
  
                  //Load in the class
                  Class&lt;?&gt; cls = cl.loadClass(classToLoad);
  
                  System.out.println("--------------------------------------------------------");
  
                  //Pass the name of the class to addClassToVariableTree in CreatUI so it can be added as a TreeItem
                  CreateMainUI.addClassToVariableTree(classToLoad);
                  //Do the same for addClassToMethodTree
                  CreateMainUI.addClassToMethodTree(classToLoad);
  
                  //Array of the declared fields in the class
                  Field[] fieldsInClass = cls.getDeclaredFields();
  
                  //For each field in fieldsInClass we add it as a TreeItem using addVariableToClassTreeItem
                  for( Field field: fieldsInClass){
                      CreateMainUI.addVariableToClassNameTreeItem(field.getGenericType().toString(), field.getName());
                  }
  
                  //Array of all methods in the class
                  Method[] methodsInClass = cls.getDeclaredMethods();
  
                  //For each method we pass through the string representation of it, the class it belongs too and it's name. It will be formatted in CreateUI
                  for (Method method : methodsInClass) {
                      CreateMainUI.addMethodToClassNameTreeItem(method.toString(), classToLoad, method.getName());
                  }
  
  
              } catch (MalformedURLException e) {
                  System.out.println("URL BAD");
              } 
              catch (ClassNotFoundException e){
                  System.out.println("Class couldnt be found");
                  e.printStackTrace();
              }
  
          }
  
          return CONTINUE;
  
      }
  
      private String getPackageName(Path filesPath){
  
          /*
           * To get the package name we will have to construct it manually. If the class
           * was already loaded we could get the package info using built in Java methods, but we
           * haven't loaded the class yet and can't without this package information hence we will
           * build a package name up be traversing back up to the bin folder adding the folder titles
           * to a String Builder as we go
           */
          File currentFolder = filesPath.toFile().getParentFile();
  
          String classesPackage = "";
  
          while(true){
  
              if(!(currentFolder.getName().equals("bin"))){
  
                  classesPackage = currentFolder.getName() + "." + classesPackage;
  
                  currentFolder = currentFolder.getParentFile();
              }
              else{
  
                  break;
              }
          }
  
          return classesPackage;
      }
  
  }
  </code></pre>
  
  <p>Console output:</p>
  
  <pre><code>Staring Directory the user selected: D:\Users\Ste\Documents\Eclipse Workspace\Project Tests\File Reader
  ----------------------------------------------------
  Parent Directory: D:\Users\Ste\Documents\Eclipse Workspace\Project Tests\File Reader\bin
  The className: ReadFile
  The classToLoad: ReadFile
  --------------------------------------------------------
  Parent Directory: D:\Users\Ste\Documents\Eclipse Workspace\Project Tests\File Reader\bin\Test
  The className: TestClass
  The classToLoad: Test.TestClass
  Class couldnt be found
  java.lang.ClassNotFoundException: Test.TestClass
      at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
      at ste.wootten.honoursproject.mainpart.reflection.ReflectOnClasses.visitFile(ReflectOnClasses.java:146)
      at ste.wootten.honoursproject.mainpart.reflection.ReflectOnClasses.visitFile(ReflectOnClasses.java:1)
      at java.nio.file.FileTreeWalker.walk(FileTreeWalker.java:135)
      at java.nio.file.FileTreeWalker.walk(FileTreeWalker.java:199)
      at java.nio.file.FileTreeWalker.walk(FileTreeWalker.java:199)
      at java.nio.file.FileTreeWalker.walk(FileTreeWalker.java:199)
      at java.nio.file.FileTreeWalker.walk(FileTreeWalker.java:69)
      at java.nio.file.Files.walkFileTree(Files.java:2591)
      at java.nio.file.Files.walkFileTree(Files.java:2624)
      at ste.wootten.honoursproject.mainpart.reflection.ReflectOnClasses.startFileWalk(ReflectOnClasses.java:41)
      at ste.wootten.honoursproject.mainpart.CreateMainUI.selectDirectoryAndBeginFileWalk(CreateMainUI.java:258)
      at ste.wootten.honoursproject.mainpart.CreateMainUI.createInterface(CreateMainUI.java:159)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:56)
      at org.eclipse.e4.core.internal.di.InjectorImpl.processAnnotated(InjectorImpl.java:859)
      at org.eclipse.e4.core.internal.di.InjectorImpl.inject(InjectorImpl.java:111)
      at org.eclipse.e4.core.internal.di.InjectorImpl.internalMake(InjectorImpl.java:319)
      at org.eclipse.e4.core.internal.di.InjectorImpl.make(InjectorImpl.java:240)
      at org.eclipse.e4.core.contexts.ContextInjectionFactory.make(ContextInjectionFactory.java:161)
      at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.createFromBundle(ReflectionContributionFactory.java:102)
      at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.doCreate(ReflectionContributionFactory.java:71)
      at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.create(ReflectionContributionFactory.java:53)
      at org.eclipse.e4.ui.workbench.renderers.swt.ContributedPartRenderer.createWidget(ContributedPartRenderer.java:141)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.createWidget(PartRenderingEngine.java:896)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:630)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:732)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.access$2(PartRenderingEngine.java:703)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$7.run(PartRenderingEngine.java:697)
      at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.createGui(PartRenderingEngine.java:682)
      at org.eclipse.e4.ui.workbench.renderers.swt.StackRenderer.showTab(StackRenderer.java:1114)
      at org.eclipse.e4.ui.workbench.renderers.swt.LazyStackRenderer$1.handleEvent(LazyStackRenderer.java:67)
      at org.eclipse.e4.ui.services.internal.events.UIEventHandler$1.run(UIEventHandler.java:41)
      at org.eclipse.swt.widgets.Synchronizer.syncExec(Synchronizer.java:180)
      at org.eclipse.swt.widgets.Display.syncExec(Display.java:4687)
      at org.eclipse.e4.ui.internal.workbench.swt.E4Application$1.syncExec(E4Application.java:187)
      at org.eclipse.e4.ui.services.internal.events.UIEventHandler.handleEvent(UIEventHandler.java:38)
      at org.eclipse.equinox.internal.event.EventHandlerWrapper.handleEvent(EventHandlerWrapper.java:197)
      at org.eclipse.equinox.internal.event.EventHandlerTracker.dispatchEvent(EventHandlerTracker.java:197)
      at org.eclipse.equinox.internal.event.EventHandlerTracker.dispatchEvent(EventHandlerTracker.java:1)
      at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
      at org.eclipse.osgi.framework.eventmgr.ListenerQueue.dispatchEventSynchronous(ListenerQueue.java:148)
      at org.eclipse.equinox.internal.event.EventAdminImpl.dispatchEvent(EventAdminImpl.java:135)
      at org.eclipse.equinox.internal.event.EventAdminImpl.sendEvent(EventAdminImpl.java:78)
      at org.eclipse.equinox.internal.event.EventComponent.sendEvent(EventComponent.java:39)
      at org.eclipse.e4.ui.services.internal.events.EventBroker.send(EventBroker.java:81)
      at org.eclipse.e4.ui.internal.workbench.UIEventPublisher.notifyChanged(UIEventPublisher.java:58)
      at org.eclipse.emf.common.notify.impl.BasicNotifierImpl.eNotify(BasicNotifierImpl.java:374)
      at org.eclipse.e4.ui.model.application.ui.impl.ElementContainerImpl.setSelectedElement(ElementContainerImpl.java:171)
      at org.eclipse.e4.ui.workbench.renderers.swt.LazyStackRenderer.postProcess(LazyStackRenderer.java:103)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:646)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:732)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.access$2(PartRenderingEngine.java:703)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$7.run(PartRenderingEngine.java:697)
      at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.createGui(PartRenderingEngine.java:682)
      at org.eclipse.e4.ui.workbench.renderers.swt.SWTPartRenderer.processContents(SWTPartRenderer.java:59)
      at org.eclipse.e4.ui.workbench.renderers.swt.PerspectiveRenderer.processContents(PerspectiveRenderer.java:59)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:642)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:732)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.access$2(PartRenderingEngine.java:703)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$7.run(PartRenderingEngine.java:697)
      at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.createGui(PartRenderingEngine.java:682)
      at org.eclipse.e4.ui.workbench.renderers.swt.PerspectiveStackRenderer.showTab(PerspectiveStackRenderer.java:103)
      at org.eclipse.e4.ui.workbench.renderers.swt.LazyStackRenderer$1.handleEvent(LazyStackRenderer.java:67)
      at org.eclipse.e4.ui.services.internal.events.UIEventHandler$1.run(UIEventHandler.java:41)
      at org.eclipse.swt.widgets.Synchronizer.syncExec(Synchronizer.java:180)
      at org.eclipse.swt.widgets.Display.syncExec(Display.java:4687)
      at org.eclipse.e4.ui.internal.workbench.swt.E4Application$1.syncExec(E4Application.java:187)
      at org.eclipse.e4.ui.services.internal.events.UIEventHandler.handleEvent(UIEventHandler.java:38)
      at org.eclipse.equinox.internal.event.EventHandlerWrapper.handleEvent(EventHandlerWrapper.java:197)
      at org.eclipse.equinox.internal.event.EventHandlerTracker.dispatchEvent(EventHandlerTracker.java:197)
      at org.eclipse.equinox.internal.event.EventHandlerTracker.dispatchEvent(EventHandlerTracker.java:1)
      at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
      at org.eclipse.osgi.framework.eventmgr.ListenerQueue.dispatchEventSynchronous(ListenerQueue.java:148)
      at org.eclipse.equinox.internal.event.EventAdminImpl.dispatchEvent(EventAdminImpl.java:135)
      at org.eclipse.equinox.internal.event.EventAdminImpl.sendEvent(EventAdminImpl.java:78)
      at org.eclipse.equinox.internal.event.EventComponent.sendEvent(EventComponent.java:39)
      at org.eclipse.e4.ui.services.internal.events.EventBroker.send(EventBroker.java:81)
      at org.eclipse.e4.ui.internal.workbench.UIEventPublisher.notifyChanged(UIEventPublisher.java:58)
      at org.eclipse.emf.common.notify.impl.BasicNotifierImpl.eNotify(BasicNotifierImpl.java:374)
      at org.eclipse.e4.ui.model.application.ui.advanced.impl.PerspectiveStackImpl.setSelectedElement(PerspectiveStackImpl.java:135)
      at org.eclipse.e4.ui.model.application.ui.advanced.impl.PerspectiveStackImpl.setSelectedElement(PerspectiveStackImpl.java:1)
      at org.eclipse.e4.ui.workbench.renderers.swt.LazyStackRenderer.postProcess(LazyStackRenderer.java:103)
      at org.eclipse.e4.ui.workbench.renderers.swt.PerspectiveStackRenderer.postProcess(PerspectiveStackRenderer.java:77)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:646)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:732)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.access$2(PartRenderingEngine.java:703)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$7.run(PartRenderingEngine.java:697)
      at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.createGui(PartRenderingEngine.java:682)
      at org.eclipse.e4.ui.workbench.renderers.swt.SWTPartRenderer.processContents(SWTPartRenderer.java:59)
      at org.eclipse.e4.ui.workbench.renderers.swt.WBWRenderer.processContents(WBWRenderer.java:639)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:642)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:732)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.access$2(PartRenderingEngine.java:703)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$7.run(PartRenderingEngine.java:697)
      at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.createGui(PartRenderingEngine.java:682)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:964)
      at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:923)
      at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:86)
      at org.eclipse.e4.ui.internal.workbench.swt.E4Application.start(E4Application.java:150)
      at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
      at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
      at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
      at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
      at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
      at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
      at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
      at org.eclipse.equinox.launcher.Main.main(Main.java:1414)
  </code></pre>
  
  <p>As you can see it finds <code>ReadFile.class</code> and loads that fine, but when I want to load <code>Test.TestClass</code> it says <code>ClassNotFoundException</code>. Any ideas??</p>