I have a program in which I am loading classes dynamically in order to pick which libraries to use at runtime. For some reason, when attempting to instantiate a direct implementer of the interface needing implementation, it throws a ClassCastException. The buggy line can be found here, but reference for below is the process which I use to load the class:
- The program starts, unaware of the separate .jars (held in another folder).
- The Collector class loads all the separate .jars' classes and dumps their references in an ArrayList.
- Another class, Prioritizer, looks in a .json file for the name of what classes to load to do which job, referring to the interface as a caster. This class then queries the Collector class for the object reference.
- The main class calls on the Prioritizer to find the right class reference for an implementation of LanguageDetail, which it then attempts to cast, then instantiate, and call a method from.
In step four, however, the cast fails and an exception is thrown.
The offending line is this:
Collector.prioritizer.get(LanguageDetail.class).newInstance().reportLanguageDetails();
However, this throws the following exception:
java.lang.ClassCastException: com.VTC.laura.test.TestLangDetails cannot be cast to com.VTC.langlib.data.LanguageDetail
at com.VTC.laura.core.Main.main(Main.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Even though TestLangDetails
is a direct implementation of LanguageDetail
.
What am I doing wrong to cause this error, and how can I fix it?
Aucun commentaire:
Enregistrer un commentaire