简体   繁体   中英

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

I have two jar files. 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.

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.

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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