简体   繁体   中英

How to call constructor from class loaded via classloader

I use classloader to load a class from a jar, but I get a "NoSuchMethodException" exception when trying to call the default constructor, which does exist.

The problem may to be that the loaded class is simply java.lang.Class?

How can I call the constructor?

Class<?> JRXlsExporter = urlClassLoader.loadClass("net.sf.jasperreports.engine.export.JRXlsExporter");
String classname = JRXlsExporter.getName();     //   looks good... it's net.sf.jasperreports.engine.export.JRXlsExporter
Class myclass = JRXlsExporter.getClass();           // this might be a a problem, the class is java.lang.Class
Constructor constructor = myclass.getConstructor();        // throws  java.lang.NoSuchMethodException: java.lang.Class.<init>()

Your JRXlsExporter is a Class - you can call getConstructor on it:

Constructor constructor = JRXlsExporter.getConstructor();

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