仅使用特定文件夹内的类创建类加载器 [英] Create Classloader with only classes inside specific folder

查看:131
本文介绍了仅使用特定文件夹内的类创建类加载器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用

此构造函数使用服务提供者机制加载对给定ClassLoader可见的ScriptEngineFactory实现.

This constructor loads the implementations of ScriptEngineFactory visible to the given ClassLoader using the service provider mechanism.

当我尝试创建 Classloader

The problem when I tried to create Classloader

File file = new File("c:\\myclasses\\");

try {
// Convert File to a URL
URL url = file.toURI().toURL();          // file:/c:/myclasses/
URL[] urls = new URL[]{url};

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

我也正在上课,例如Spring

And I'm getting also class that weren't in class for example Spring

 Class cls1 = cl.loadClass("org.springframework.http.HttpStatus");

如何仅使用特定文件夹的类来创建干净的类加载器?

How can I create a clean classloader with only specific folder's classes?

编辑

如果不可能,我可以在groovy脚本中使用与Rhino的 ClassShutter

If it's not possible, can I use in groovy script something equivalent to Rhino's ClassShutter

private static void setJavaClassesVisibleInvisibleSandbox(Context cx)
 {
  cx.setClassShutter(new ClassShutter()
   {
      public boolean visibleToScripts(String className)
      {
          // No Java classes allowed inside scripts

编辑

当尝试使用@ Vinz243 CustomClassLoader解决方案无法从特定文件夹加载类文件或jar文件的类时,甚至尝试了rt.jar

When tried @Vinz243 CustomClassLoader solution failed to load classes for classes files or jar files from specific folder, even tried rt.jar

java.lang.SecurityException: Prohibited package name: java.lang
    at java.lang.ClassLoader.preDefineClass(ClassLoader.java:662)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:761)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at com.CustomClassLoader.loadClass(CustomClassLoader.java:15)

推荐答案

如javadoc中所指定:

As specified in the javadoc :

public URLClassLoader(URL[] urls)

public URLClassLoader(URL[] urls)

使用默认的委托父ClassLoade r为指定的URL 构造一个新的URLClassLoader.在首先在父类加载器中搜索后,将按照为类和资源指定的顺序搜索URL.假定任何以"/"结尾的URL均指向目录.否则,假定该URL引用了一个JAR文件,该文件将根据需要下载并打开.

Constructs a new URLClassLoader for the specified URLs using the default delegation parent ClassLoader. The URLs will be searched in the order specified for classes and resources after first searching in the parent class loader. Any URL that ends with a '/' is assumed to refer to a directory. Otherwise, the URL is assumed to refer to a JAR file which will be downloaded and opened as needed.

因此,您可以尝试扩展ClassLoader,因为负责加载父类的机制位于java.lang.ClassLoader#loadClass(java.lang.String, boolean)

So you can try to extend ClassLoader since the mechanism responsible for loading the parent class is inside java.lang.ClassLoader#loadClass(java.lang.String, boolean)

    try {
        if (parent != null) {
            c = parent.loadClass(name, false);
        } else {
            c = findBootstrapClassOrNull(name);
        }
    } catch (ClassNotFoundException e) {
        // ClassNotFoundException thrown if class not found
        // from the non-null parent class loader
    }

编辑

未经测试,但是类似的事情应该可以完成:

Not tested, but something like that should do the job:

class CustomClassLoader extends URLClassLoader {

    public CustomClassLoader (URL[] urls) throws NoSuchMethodException {
        super(urls);
    }

    @Override
    protected Class<?> loadClass (String name, boolean resolve) throws ClassNotFoundException {
        synchronized (getClassLoadingLock(name)) {
            Class<?> aClass = findClass(name);
            if (resolve) {
                resolveClass(aClass);
            }
            return aClass;
        }
    }
}

这篇关于仅使用特定文件夹内的类创建类加载器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆