我试图在运行时使用反射动态链接到jar文件。我已经创建了主类和方法的声明,并且它的工作正常,除非我尝试调用方法时,我得到了这个异常:
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)
有问题的一行是:
final Reflections reflections = new Reflections(null, new ResourcesScanner());
我尝试将反射jar声明为对主应用程序的直接依赖,希望它会加载它,但这没有帮助。我不知道还能做什么?使用反射加载依赖项时是否有技巧?
答案 0 :(得分:1)
“当Java虚拟机(JVM)尝试加载特定类并且在类路径中找不到指定的类时,抛出ClassNotFoundException”
解析org.reflections.Reflections的依赖项。希望您已下载所需的jar文件并将其包含在项目类路径中。或者,您可以使用maven。