在FileWalker中使用ClassLoader的ClassNotFoundException

时间:2013-03-01 17:19:01

标签: java classloader classnotfoundexception

我有一个程序,用户选择一个目录来开始FileWalk; FileWalker访问目录结构中的每个文件,并使用.class加载CLassLoader文件,因此我可以使用Reflection在GUI中显示有关该类的信息。

如果用户选择具有此结构的文件夹FileReader:

D:\Users\Ste\Documents\Eclipse Workspace\Project Tests\File Reader
    │
    ├───+bin
    │   │   ReadFile.class   
    |
    │
    └───+src
        │   ReadFile.java

ClassLoader加载ReadFile.class

没有问题

但是,如果我在包测试中选择了添加了类的相同目录:

D:\Users\Ste\Documents\Eclipse Workspace\Project Tests\File Reader
    │
    ├───+bin
    │   │   ReadFile.class
    │   │
    │   └───+Test
    │           TestClass.class
    │
    └───+src
        │   ReadFile.java
        │
        └───+Test
                TestClass.java

Eclipse抛出java.lang.ClassNotFoundException: Test.TestClass

那我为什么要java.lang.ClassNotFoundException?下面是我的代码,其中包含一些System.out.println()来显示一些值。 (目录结构与上面的结构相同)

public class ReflectOnClasses extends SimpleFileVisitor<Path> {

    //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<?> 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;
    }

}

控制台输出:

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)

正如您所见,它找到ReadFile.class并加载正常,但当我想加载Test.TestClass时,它会显示ClassNotFoundException。任何想法??

1 个答案:

答案 0 :(得分:1)

当你走TestFile.class

File parentDir = new File(filesPath.getParent().toString());
System.out.println("Parent Directory: " + filesPath.getParent().toString());

在此之后,您有parentDir = "D:\Users\Ste\Documents\Eclipse Workspace\Project Tests\File Reader\bin\Test"

URL url = parentDir.toURL();
URL[] urls = new URL[]{url};
//Create a new class loader with the directory
ClassLoader cl = new URLClassLoader(urls);

在此之后,您的类加载器搜索目录为file:///D:/Users/Ste.../bin/Test,这是不正确的(此处没有/Test/TestClass.class来匹配已编译的Test.TestClass

在这种情况下,您应该从startingDir + "/bin"创建类加载器,而不是从parentDir创建。

如果您需要帮助来了解正在发生的事情,这是一个非常基本的Java Package Tutorial