简体   繁体   English

在JAVA运行时从另一个类加载器获取对类的引用

[英]Get reference to Class from another class loader at runtime in JAVA

I am running some domain specific scripts in a new thread of an application. 我在应用程序的新线程中运行某些特定于域的脚本。 The classloader in the thread only has all the classes of the application. 线程中的类加载器仅具有应用程序的所有类。

The application also has a module that loads additional classes in jars in a folder at runtime (classes created outside of the application), providing additional functionalities to other parts of the application. 该应用程序还具有一个模块,该模块在运行时将其他类加载到文件夹中的jar中(在应用程序外部创建的类),从而为应用程序的其他部分提供其他功能。

Unfortunately, these two classloaders are not merged and when running my scripts I cannot load any class that I know is there in one of the external jars. 不幸的是,这两个类加载器没有合并,并且在运行脚本时,我无法加载任何我知道存在于外部jar中的类。

Tried this.getClass().getClassLoader().loadClass("external.cls") no go. 试过this.getClass().getClassLoader().loadClass("external.cls")不行。

I also tried Reflections reflections = new Reflections("external.cls") without success. 我也尝试了Reflections reflections = new Reflections("external.cls")但没有成功。

Is there a way to gain access to those classes in my script running thread at runtime? 有没有办法在运行时在脚本运行线程中获得对这些类的访问? Something like: 就像是:

  • Gain access to another class loader that loaded the external class? 是否可以访问另一个已加载外部类的类加载器?
  • Use reflection to get the external class from the two class loaders? 使用反射从两个类加载器获取外部类?
  • Load the external jars again in my script running thread? 在我的脚本运行线程中再次加载外部jar?

Most of the options you outlined in your question would work. 您在问题中概述的大多数选项都可以使用。

Use reflection to get the external class from the two class loaders? 使用反射从两个类加载器获取外部类?

Would not. 不会。 Class loading is about finding classes in the classpath, and making them available. 类加载是关于在类路径中查找类,并使它们可用。 Reflection doesn't help there. 反思无济于事。 It doesn't matter if you load a class because it is directly referenced in a class file; 加载类并不重要,因为直接在类文件中引用了它。 or because you call Class.forName(). 或因为您调用Class.forName()。

For the other two options: 对于其他两个选项:

Load the external jars again in my script running thread? 在我的脚本运行线程中再次加载外部jar?

is probably the "easiest" to implement; 可能是“最容易实现”的; but of course, it means re-loading classes; 当然,这意味着要重新加载类; in other words: doing work again. 换句话说:再做一次工作。 Depending on the amount of classes we are talking about, this could be insignificant, or pretty costly. 根据我们正在谈论的课程数量,这可能是微不足道的,或者是非常昂贵的。

Gain access to another class loader that loaded the external class? 是否可以访问另一个已加载外部类的类加载器?

would require you to rework that "script class" loader. 将需要您重新处理该“脚本类”加载器。 When it doesn't find a class, it has to delegate the loadClass() call to those "other" class loaders. 当找不到类时,它必须将loadClass()调用委托给那些“其他”类加载器。

Most likely, your first option could be implemented in a clean, straight forward way - but it doesn't come for free; 最有可能的是,您的第一个选择可能会以一种干净,直接的方式实施-但它并非免费提供; you have to write the code to make that happen. 必须编写代码才能实现这一目标。

Create a static reference to the additional class loader in one of your main application's classes. 在您的主应用程序的一个类中创建对其他类加载器的静态引用。 When loading additional classes for the first time, set it up. 首次加载其他类时,进行设置。 When running scripts, it will already be there to provide you with additional classes, already loaded and cached by the JVM, or to load more of them not yet loaded. 在运行脚本时,它已经可以为您提供由JVM已经加载和缓存的其他类,或者加载更多尚未加载的类。 Such a dependency of the main class loader on the additional one will correctly reflect the fact that the application depends on the additional module. 主类加载器对附加模块的这种依赖关系将正确反映应用程序依赖于附加模块的事实。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM