简体   繁体   English

如何用Java中的其他ClassLoader重新加载已加载的类?

[英]How to reload a loaded class with different ClassLoader in Java?

public class ClassScanner
{
  // scan extraClasspath for specific classes
  public List<Class<?>> scanClasspathForClass(String scanCriteria)
  {
    ...
  }

  public static Class<?> reloadClass(Class<?> clazz, ClassLoader clazzLoader)
  {
    // Question: how to reload a loaded class (ClassScanner in this example) with a different arbitrary ClassLoader?
  }

  // an example of how reloadClass() would be used
  // in real case, this function would be in different class
  public List<Class<?>> scan(URL[] extraClasspath)
  {
    URLClassLoader urlClazzLoader = new URLClassLoader(extraClasspath, null);
    Class<?> newClass = reloadClass(ClassScanner.class, urlClazzLoader);
    return ((ClassScanner) newClass.newInstance()).scanClasspathForClass();
  }
}

Above code demonstrates the question and why it is a question. 上面的代码演示了问题以及为什么是问题。 I need to implement reloadClass() . 我需要实现reloadClass() I wonder if there is a reliable solution in Java 1.6. 我想知道Java 1.6中是否有可靠的解决方案。 One useful reference would be Find where java class is loaded from . 一个有用的参考是查找从中加载java类的位置

Thanks for help! 感谢帮助!

Found myself the answer from http://www2.sys-con.com/itsg/virtualcd/java/archives/0808/chaudhri/index.html . http://www2.sys-con.com/itsg/virtualcd/java/archives/0808/chaudhri/index.html找到了答案。

Basically what I need is to make one ClassLoader A to share its namespace with another ClassLoader B. The way I found to achieve this is to use the ClassLoader parent-delegation model. 基本上,我需要使一个ClassLoader A与另一个ClassLoader B共享其名称空间。我发现实现此目标的方法是使用ClassLoader父委托模型。 Here, ClassLoader A is the parent ClassLoader. 在这里,ClassLoader A是父ClassLoader。

public List<Class<?>> scan(URL[] extraClasspath) throws Exception
{
  URLClassLoader urlClazzLoader = new URLClassLoader(extraClasspath, ClassScanner.class.getClassLoader());
  return urlClazzLoader.loadClass(ClassScanner.getName()).newInstance();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用加载不同Java类加载器的实现? - How to use implementation loaded with different Java classloader? 重新加载使用自定义类加载器加载的类 - Reload class which is loaded using custom classloader 如何验证Java中的ClassLoader是否已加载.jar或类 - How to verify that a .jar or a class has been already loaded by the ClassLoader in Java 为什么这个class可以被Java类加载器加载两次? - Why this class can be loaded twice by Java classloader? Java有没有办法加载一个已被System ClassLoader加载的类不同的类? - Java is there a way to load a class that has been already loaded by the System ClassLoader by a different one? 如何声明由类加载器加载的 class 创建的 object - How to declare an object created by a class loaded by classloader 如何使用反射检查加载了不同ClassLoader的类的非静态字段? - How to use Reflection to inspect non-static fields of a class that is loaded with a different ClassLoader? 如何在第一次加载时让Java ClassLoader输出每个类 - How do I get Java ClassLoader to output each class when loaded for the first time AspectJ 自动代理在 GigaSpaces 8 和 Spring 3 中失败,因为类是由不同的类加载器加载的 - AspectJ autoproxy fails with GigaSpaces 8 and Spring 3 because class is loaded by different classloader Java ClassLoader - 将动态加载的 jars 添加到系统 class 加载器 - Java ClassLoader - Adding dynamically loaded jars to the system class loader
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM