1

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?

3
  • Why all these class loading questions lately? You won't improve on the JVM class loader. Use it. Commented Jun 29, 2016 at 13:54
  • It looks like you don't have the jar that has org.reflections. Reflections on your class path at runtime. Check your runtime classpath Commented Jun 29, 2016 at 13:57
  • @ControlAltDel that was the issue, I forgot that I was using maven-dependancy-plugin and that I had to copy the jar files to my dependencies directory. Commented Jul 3, 2016 at 7:30

1 Answer 1

1

"The ClassNotFoundException is thrown when the Java Virtual Machine (JVM) tries to load a particular class and the specified class cannot be found in the classpath"

Resolve the dependencies for org.reflections.Reflections. Hope you have downloaded required jar files and included them in your project classpath. Alternatively, you can use maven for that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.