简体   繁体   English

如何在运行时从文件夹加载jar文件并实例化JBoss EAP 6.0.1中的类

[英]How to load jar files at runtime from a folder & instantiate classes in JBoss EAP 6.0.1

I have two jar files. 我有两个jar文件。 First contains only the interfaces & the second contains all the implementation classes. 第一个仅包含接口,第二个包含所有实现类。 I have included the first jar in the ear file of my application, so its classes are getting loaded when my application is deployed. 我将第一个jar包含在应用程序的ear文件中,因此在部署应用程序时将加载其类。

On the request basis I am trying to load my implementation classes from .jar file present in some folder, at run time using the URLClassLoader. 根据请求,我试图在运行时使用URLClassLoader从某个文件夹中的.jar文件加载实现类。

The classes are getting loaded & using reflection I am creating the instance of class, but while casting that instance to the interface present in jar file which I included in the .ear files, I am getting NoClassDefFoundError for my interface class. 我正在创建类的实例,但是正在使用我的反射来加载类,但是在将该实例转换为包含在.ear文件中的jar文件中存在的接口时,我的接口类却收到了NoClassDefFoundError。

I am sure this is due to different class loaders I am using to load the classes, but how do I overcome this issue of compatibility between the classes. 我确定这是由于我用于加载类的类加载器不同而引起的,但是如何克服类之间的兼容性问题。

Same code works in normal J2SE environment, but its not working in JBoss camel context. 相同的代码在正常的J2SE环境中有效,但在JBoss骆驼上下文中无效。

If my query is not clear, please let me know I can paste the source code also. 如果我的查询不清楚,请让我知道我也可以粘贴源代码。

Your method should works on JBossEAP 6, that's what I did on JBossAS7.1. 您的方法应该在JBossEAP 6上有效,这就是我在JBossAS7.1上所做的工作。

class YourClassLoader extends URLClassLoader {
    ...
    public YourClassLoader() {
       this(YourClassLoader.class.getClassLoader());
       ...
       this.addURL(jarfile.toURI().toURL());
       ...
    }
    ...
}

Class<?> yourClass = yourClassLoader.loadClass(className);
YourClassInterface yourClassInterface = null;
yourClassInterface = (YourClassInterface ) yourClass.newInstance();

And please try load the interfaces jar by the same one URLClassLoader. 并且请尝试使用相同的URLClassLoader加载接口jar。

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

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