mercredi 29 juin 2016

Java reflection ClassNotFound Exception

I am trying to dynamically link to a jar file at run time using reflections. I have created the declarations for the main classes and the methods and its all working fine except when I try to invoke a method, I get this exception:

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.myapp.languagedetectorreflection.LangDetectReflectionManager.CreateDetectorFactory(LangDetectReflectionManager.java:73)
    at com.myapp.sharedlibrary.LanguageManager.LanguageDetector.<init>(LanguageDetector.java:40)
    at com.myapp.sharedlibrary.LanguageManager.LanguageDetector.getInstance(LanguageDetector.java:50)
    at com.myapp.tikamanager.MetaParser.<init>(MetaParser.java:66)
    at com.myapp.langtest.LangTest.main(LangTest.java:85)
Caused by: java.lang.NoClassDefFoundError: org/reflections/Reflections
    at com.cybozu.labs.langdetect.DetectorFactory.<init>(DetectorFactory.java:77)
    ... 9 more
Caused by: java.lang.ClassNotFoundException: org.reflections.Reflections
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at com.myapp.languagedetectorreflection.ParentLastURLClassLoader.loadClass(ParentLastURLClassLoader.java:70)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

The line in question is: final Reflections reflections = new Reflections(null, new ResourcesScanner());

I tried declare the reflections jar as a direct dependency to the main application hoping that it will load it but that did not help. I am not sure what else to do? Is there a trick when using reflections to load dependencies?





Aucun commentaire:

Enregistrer un commentaire