自定义classLoader问题

自定义classLoader问题

本文介绍了自定义classLoader问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是下一步:我从获取基本的classLoader代码。但我的classLoader是从一个点,它必须能够从文件系统加载类(让我们接受WinOS),所以在classLoader必须有一些 setAdditionalPath()方法,它设置一个路径(文件系统上的一个目录),从中我们将加载类(只有* .class,没有jars)。这里是代码,它修改加载器从链接(你可以看到,只有loadClass被修改),但它不能正常工作:

the problem is next: i took the base classLoader code from here. but my classLoader is specific from a point, that it must be able to load classes from a filesystem(let's take WinOS), so in classLoader must be some setAdditionalPath() method, which sets a path(a directory on a filesystem), from which we'll load class(only *.class, no jars). here is code, which modifies the loader from a link(you can see, that only loadClass is modified), but it doesn't work properly:

public void setAdditionalPath(String dir) {
            if(dir == null) {
                throw new NullPointerException("");
            }

            this.Path = dir;
        }

        public Loader(){
              super(Loader.class.getClassLoader());
        }


        public Class loadClass(String className) throws ClassNotFoundException {
          if(Path.length() != 0) {
            File file = new File(Path);

            try {
                // Convert File to an URL

         URL url = file.toURL();
                URL[] urls = new URL[]{url};

                // Create a new class loader with the directory
                ClassLoader cl = new URLClassLoader(urls);


                ClassLoader c = cl.getSystemClassLoader();
                Class cls = c.loadClass(className);
                return cls;

            } catch (MalformedURLException e) {

            } catch (ClassNotFoundException e) {

            }

        }
            return findClass(Path);
        }

我很感激任何人帮助:)

I'd grateful if anyone helps :)

推荐答案

您可以使用提供的框架。没有必要自己写。它支持从目录和JAR文件加载类。

You can just use framework provided java.net.URLClassLoader. No need to write your own. It supports loading of classes from directories and JAR files.

它还支持父类加载器。如果这个类加载器不满足你的要求,也许你可以更详细地指定你需要什么。在任何情况下,你可以查看源代码并根据这些来导出你自己的类加载器类。

It also supports a parent class loader. If this class loader does not suite your requirements, perhaps you can specify in more detail what you need. And in any case, you can look at the source and derive your own class loader class based on that.

这里是一个简短的代码工作片段,应该演示如何从URLClassLoader加载类名:

Here is a short working snippet of code that should demostrate how to load a class by name from a URLClassLoader:

    ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();

    // This URL for a directory will be searched *recursively*
    URL classes =
        new URL( "file:///D:/code/myCustomClassesAreUnderThisFolder/" );

    ClassLoader custom =
        new URLClassLoader( new URL[] { classes }, systemClassLoader );

    // this class should be loaded from your directory
    Class< ? > clazz = custom.loadClass( "my.custom.class.Name" );
    // this class will be loaded as well, because you specified the system
    // class loader as the parent
    Class< ? > clazzString = custom.loadClass( "java.lang.String" );

这篇关于自定义classLoader问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:46