java.lang.ClassNotFoundException 在 spring 启动时 运行 jar 文件 - 添加库

java.lang.ClassNotFoundException in spring boot when running jar file - Library is added

我正在将 jfuzzylite-5.0.1.jar 库导入我的 spring 启动项目。我的图书馆是用这种方式导入的:

<dependency>
    <groupId>com.fuzzylite</groupId>
    <artifactId>jfuzzylite</artifactId>
    <version>5.0.1</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/libs/jfuzzylite-5.0.1.jar</systemPath>
</dependency>

当我使用该库在我的项目中编写一个 class 时,它起作用了。我在我的控制器中使用它。

构建项目时,没有出现错误,BUILD SUCCESS。

但是在 tomcat 服务器上部署或执行 jar 文件时,出现以下错误:

  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.4.3.RELEASE)

2017-01-15 09:33:03.431  INFO 6944 --- [           main] com.fuzzy.FuzzyTcmApplication            : Starting FuzzyTcmApplication v0.1 on DESKTOP-8L48946 with PID 6944 (C:\DATA\DATA\G
IT_PROJECTS\fuzzy-tcm\Sourcode\fuzzy-tcm-back-end\target\fuzzy-tcm-0.1.jar started by PC in C:\DATA\DATA\GIT_PROJECTS\fuzzy-tcm\Sourcode\fuzzy-tcm-back-end\target)
2017-01-15 09:33:03.440  INFO 6944 --- [           main] com.fuzzy.FuzzyTcmApplication            : No active profile set, falling back to default profiles: default
2017-01-15 09:33:03.642  INFO 6944 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebAp
plicationContext@5b37e0d2: startup date [Sun Jan 15 09:33:03 ICT 2017]; root of context hierarchy
2017-01-15 09:33:06.439  INFO 6944 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-01-15 09:33:06.465  INFO 6944 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2017-01-15 09:33:06.468  INFO 6944 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.6
2017-01-15 09:33:06.635  INFO 6944 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-01-15 09:33:06.636  INFO 6944 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3029 ms
2017-01-15 09:33:06.898  INFO 6944 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-01-15 09:33:06.912  INFO 6944 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-01-15 09:33:06.913  INFO 6944 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-01-15 09:33:06.913  INFO 6944 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-01-15 09:33:06.913  INFO 6944 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-01-15 09:33:06.977  WARN 6944 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fuzzyServices': Failed to introspect bean class [com.fuzzy.controller.FuzzyServices] for looku
p method metadata: could not find class that it depends on; nested exception is java.lang.NoClassDefFoundError: com/fuzzylite/term/Term
2017-01-15 09:33:06.984  INFO 6944 --- [           main] o.apache.catalina.core.StandardService   : Stopping service Tomcat
2017-01-15 09:33:07.013  INFO 6944 --- [           main] utoConfigurationReportLoggingInitializer :

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-01-15 09:33:07.028 ERROR 6944 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fuzzyServices': Failed to introspect bean class [com.fuzzy.controller.FuzzyServices] for looku
p method metadata: could not find class that it depends on; nested exception is java.lang.NoClassDefFoundError: com/fuzzylite/term/Term
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:269) ~[spring-be
ans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1079) ~[sp
ring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1052) ~[spring-beans-4.3.5.RELEASE.
jar!/:4.3.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.5.RELEASE.jar!/:4
.3.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3
.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE
]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5
.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.5.RELEASE.jar!/:4.
3.5.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.3.RELEASE.jar!/:1.4.3.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) [spring-boot-1.4.3.RELEASE.jar!/:1.4.3.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) [spring-boot-1.4.3.RELEASE.jar!/:1.4.3.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.4.3.RELEASE.jar!/:1.4.3.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) [spring-boot-1.4.3.RELEASE.jar!/:1.4.3.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) [spring-boot-1.4.3.RELEASE.jar!/:1.4.3.RELEASE]
        at com.fuzzy.FuzzyTcmApplication.main(FuzzyTcmApplication.java:10) [classes!/:0.1]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_101]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_101]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_101]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_101]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [fuzzy-tcm-0.1.jar:0.1]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [fuzzy-tcm-0.1.jar:0.1]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [fuzzy-tcm-0.1.jar:0.1]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [fuzzy-tcm-0.1.jar:0.1]
Caused by: java.lang.NoClassDefFoundError: com/fuzzylite/term/Term
        at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_101]
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source) ~[na:1.8.0_101]
        at java.lang.Class.getDeclaredMethods(Unknown Source) ~[na:1.8.0_101]
        at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:613) ~[spring-core-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
        at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:524) ~[spring-core-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
        at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:510) ~[spring-core-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:247) ~[spring-be
ans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
        ... 26 common frames omitted
Caused by: java.lang.ClassNotFoundException: com.fuzzylite.term.Term
        at java.net.URLClassLoader.findClass(Unknown Source) ~[na:1.8.0_101]
        at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_101]
        at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:94) ~[fuzzy-tcm-0.1.jar:0.1]
        at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_101]
        ... 33 common frames omitted

我附加了 jar 文件,包括库。可能是什么问题?

原因可能是明确的 -

<scope>system</scope>

This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.

如果您在系统中没有明确的工件 JAR,您应该尝试使用默认范围

<scope>compile</scope> <!-- you can avoid using scope entirely-->

This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.

此外,避免使用 systemPath 和使用 ${project.basedir} 等的相对路径值,因为它们现在在 maven 中已被弃用。

来源 - Dependency Scope in Maven and All Variables