java.lang.ClassNotFoundException 运行 jar 文件时 Spring Boot 中的 java.lang.ClassNotFoundException - 添加库

Posted

技术标签:

【中文标题】java.lang.ClassNotFoundException 运行 jar 文件时 Spring Boot 中的 java.lang.ClassNotFoundException - 添加库【英文标题】:java.lang.ClassNotFoundException in spring boot when running jar file - Library is added 【发布时间】:2017-05-30 03:56:37 【问题描述】:

我正在将 jfuzzylite-5.0.1.jar 库导入我的 Spring Boot 项目。我的库是使用这种方式导入的:

<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>

当我使用该库在我的项目中编写一个类时,它可以工作。我在控制器中使用它。

构建项目时,没有出现错误,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$1.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 文件,包括库。可能是什么问题?

【问题讨论】:

【参考方案1】:

原因可能是明确的 -

<scope>system</scope>

此范围与提供的范围类似,只是您必须提供 明确包含它的 JAR。工件始终可用并且 不在存储库中查找。

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

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

这是默认范围,如果未指定则使用。编译 依赖项在项目的所有类路径中都可用。 此外,这些依赖项会传播到相关项目。

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

来源 - Dependency Scope in Maven 和 All Variables

【讨论】:

当我使用“complie”和其他范围时,会发生此错误:com.fuzzylite:jfuzzylite:jar 的“dependencies.dependency.systemPath”必须省略。只能为具有系统范围的依赖项指定此字段。 因为systemPath 应该只与system 范围一起使用。对于任何其他范围,请同时删除 systemPath 属性。 感谢您的帮助。因为这个版本的库在 maven repo 上不存在。所以我创建了一个不同的本地 Maven 存储库。我只是删除 systemPath 并使用另一种方式。现在可以了。

以上是关于java.lang.ClassNotFoundException 运行 jar 文件时 Spring Boot 中的 java.lang.ClassNotFoundException - 添加库的主要内容,如果未能解决你的问题,请参考以下文章