由以下原因引起:java.lang.ClassNotFoundException:升级到 Java 11 后 IntelliJ 中的 com.fasterxml.jackson.databind.ObjectMapper

Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ObjectMapper in IntelliJ after upgrading to Java 11

升级到 Java 11 和 Spring Boot 2.2.6 / Jackson 2.10.3 后,我无法在 Intellij 中启动我的应用程序。 运行 用 Maven 编译后 java -jar app.jar 它工作得很好。

尽管在 IntelliJ 中尝试 运行 时,我收到以下错误:Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ObjectMapper

这是完整的 Stacktrace:

2020-04-28 13:07:36.079 ERROR 83492 --- [lication Thread] o.s.b.web.embedded.tomcat.TomcatStarter  : Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'servletEndpointRegistrar' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: Factory method 'servletEndpointRegistrar' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configurationPropertiesReportEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/context/properties/ConfigurationPropertiesReportEndpointAutoConfiguration.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.boot.actuate.context.properties.ConfigurationPropertiesReportEndpoint] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@6adede5]
2020-04-28 13:07:36.092  INFO 83492 --- [lication Thread] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-04-28 13:07:36.099 ERROR 83492 --- [lication Thread] org.apache.catalina.core.ContainerBase   : A child container failed during stop

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'servletEndpointRegistrar' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: Factory method 'servletEndpointRegistrar' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configurationPropertiesReportEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/context/properties/ConfigurationPropertiesReportEndpointAutoConfiguration.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.boot.actuate.context.properties.ConfigurationPropertiesReportEndpoint] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@6adede5]
    at spring.beans@5.2.5.RELEASE/org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:656)
    at spring.beans@5.2.5.RELEASE/org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:636)
    at spring.beans@5.2.5.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338)
    at spring.beans@5.2.5.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
    at spring.beans@5.2.5.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
    at spring.beans@5.2.5.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
    at spring.beans@5.2.5.RELEASE/org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=13=](AbstractBeanFactory.java:323)
    at spring.beans@5.2.5.RELEASE/org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at spring.beans@5.2.5.RELEASE/org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
    at spring.beans@5.2.5.RELEASE/org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:207)
    at spring.boot@2.2.6.RELEASE/org.springframework.boot.web.servlet.ServletContextInitializerBeans.getOrderedBeansOfType(ServletContextInitializerBeans.java:211)
    at spring.boot@2.2.6.RELEASE/org.springframework.boot.web.servlet.ServletContextInitializerBeans.getOrderedBeansOfType(ServletContextInitializerBeans.java:202)
    at spring.boot@2.2.6.RELEASE/org.springframework.boot.web.servlet.ServletContextInitializerBeans.addServletContextInitializerBeans(ServletContextInitializerBeans.java:96)
    at spring.boot@2.2.6.RELEASE/org.springframework.boot.web.servlet.ServletContextInitializerBeans.<init>(ServletContextInitializerBeans.java:85)
    at spring.boot@2.2.6.RELEASE/org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getServletContextInitializerBeans(ServletWebServerApplicationContext.java:253)
    at spring.boot@2.2.6.RELEASE/org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.selfInitialize(ServletWebServerApplicationContext.java:227)
    at spring.boot@2.2.6.RELEASE/org.springframework.boot.web.embedded.tomcat.TomcatStarter.onStartup(TomcatStarter.java:53)
    at org.apache.tomcat.embed.core@9.0.33/org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5140)
    at org.apache.tomcat.embed.core@9.0.33/org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.tomcat.embed.core@9.0.33/org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384)
    at org.apache.tomcat.embed.core@9.0.33/org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at org.apache.tomcat.embed.core@9.0.33/org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
    at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:140)
    at org.apache.tomcat.embed.core@9.0.33/org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909)
    at org.apache.tomcat.embed.core@9.0.33/org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:841)
    at org.apache.tomcat.embed.core@9.0.33/org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.tomcat.embed.core@9.0.33/org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384)
    at org.apache.tomcat.embed.core@9.0.33/org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at org.apache.tomcat.embed.core@9.0.33/org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
    at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:140)
    at org.apache.tomcat.embed.core@9.0.33/org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909)
    at org.apache.tomcat.embed.core@9.0.33/org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)
    at org.apache.tomcat.embed.core@9.0.33/org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.tomcat.embed.core@9.0.33/org.apache.catalina.core.StandardService.startInternal(StandardService.java:421)
    at org.apache.tomcat.embed.core@9.0.33/org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.tomcat.embed.core@9.0.33/org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:930)
    at org.apache.tomcat.embed.core@9.0.33/org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.tomcat.embed.core@9.0.33/org.apache.catalina.startup.Tomcat.start(Tomcat.java:468)
    at spring.boot@2.2.6.RELEASE/org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:107)
    ... 19 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: Factory method 'servletEndpointRegistrar' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configurationPropertiesReportEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/context/properties/ConfigurationPropertiesReportEndpointAutoConfiguration.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.boot.actuate.context.properties.ConfigurationPropertiesReportEndpoint] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@6adede5]
    at spring.beans@5.2.5.RELEASE/org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
    at spring.beans@5.2.5.RELEASE/org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651)
    ... 59 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configurationPropertiesReportEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/context/properties/ConfigurationPropertiesReportEndpointAutoConfiguration.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.boot.actuate.context.properties.ConfigurationPropertiesReportEndpoint] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@6adede5]
    at spring.beans@5.2.5.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:572)
    at spring.beans@5.2.5.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
    at spring.beans@5.2.5.RELEASE/org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=13=](AbstractBeanFactory.java:323)
    at spring.beans@5.2.5.RELEASE/org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at spring.beans@5.2.5.RELEASE/org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
    at spring.beans@5.2.5.RELEASE/org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at spring.context@5.2.5.RELEASE/org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1108)
    at spring.boot.actuator@2.2.6.RELEASE/org.springframework.boot.actuate.endpoint.annotation.EndpointDiscoverer.createEndpointBean(EndpointDiscoverer.java:143)
    at spring.boot.actuator@2.2.6.RELEASE/org.springframework.boot.actuate.endpoint.annotation.EndpointDiscoverer.createEndpointBeans(EndpointDiscoverer.java:133)
    at spring.boot.actuator@2.2.6.RELEASE/org.springframework.boot.actuate.endpoint.annotation.EndpointDiscoverer.discoverEndpoints(EndpointDiscoverer.java:122)
    at spring.boot.actuator@2.2.6.RELEASE/org.springframework.boot.actuate.endpoint.annotation.EndpointDiscoverer.getEndpoints(EndpointDiscoverer.java:116)
    at spring.boot.actuator.autoconfigure@2.2.6.RELEASE/org.springframework.boot.actuate.autoconfigure.endpoint.web.ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration.servletEndpointRegistrar(ServletEndpointManagementContextConfiguration.java:65)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at spring.beans@5.2.5.RELEASE/org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    ... 60 more
Caused by: java.lang.IllegalStateException: Failed to introspect Class [org.springframework.boot.actuate.context.properties.ConfigurationPropertiesReportEndpoint] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@6adede5]
    at spring.core@5.2.5.RELEASE/org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:481)
    at spring.core@5.2.5.RELEASE/org.springframework.util.ReflectionUtils.doWithLocalMethods(ReflectionUtils.java:321)
    at spring.beans@5.2.5.RELEASE/org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.buildLifecycleMetadata(InitDestroyAnnotationBeanPostProcessor.java:232)
    at spring.beans@5.2.5.RELEASE/org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.findLifecycleMetadata(InitDestroyAnnotationBeanPostProcessor.java:210)
    at spring.beans@5.2.5.RELEASE/org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(InitDestroyAnnotationBeanPostProcessor.java:149)
    at spring.context@5.2.5.RELEASE/org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(CommonAnnotationBeanPostProcessor.java:310)
    at spring.beans@5.2.5.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:1094)
    at spring.beans@5.2.5.RELEASE/org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:569)
    ... 76 more
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper
    at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
    at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3167)
    at java.base/java.lang.Class.getDeclaredMethods(Class.java:2310)
    at spring.core@5.2.5.RELEASE/org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:463)
    ... 83 more
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ObjectMapper
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 87 more
Exception in Application stop method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application stop method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:905)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.NullPointerException
    at gui/de.company.project.MyMainClass.stop(MyMainClass.java:87)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1(LauncherImpl.java:865)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop(WinApplication.java:174)
    ... 1 more
Exception running application de.company.project.MyMainClass

这是我的家属:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.10.3</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.2.6.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
    <version>2.2.6.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>2.2.6.RELEASE</version>
</dependency>

我已经尝试删除 Jackson 依赖项,但没有任何变化。

我觉得问题很简单。您正在从您的代码生成一个 jar,但问题是当创建这样的 jar 时,您需要注意在 CLASSPATH 中提供您的第 3 方依赖项,否则如果在执行您的 jar 时找不到所需的,则可能会抛出上述错误外部 类.

现在让我们看看您上面描述的两种方式:

  1. java -jar app.jar 这将打包您当前的代码并需要将适当的文件添加到 CLASSPATH。

  2. 现在,当您通过 Maven 生成 jar 时,它会下载所需的第 3 方 类,然后将您的应用程序打包到之前提到的已下载 类 的 jar 中。因此,当 运行 那个 jar 时,所有需要的 类 都会出现。

问题是我的 jackson 依赖项中的 <scope>compile</scope> 丢失了。 它是这样工作的:

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.11.0</version>
        <scope>compile</scope>
    </dependency>

我还必须添加具有相同范围的 classmate

    <dependency>
        <groupId>com.fasterxml</groupId>
        <artifactId>classmate</artifactId>
        <version>1.5.1</version>
        <scope>compile</scope>
    </dependency>

最后,我不得不将这两个条目添加到我的 module-info.java 文件中:

open module gui {
    ...
    requires com.fasterxml.classmate;
}

module core_entity {

    requires com.fasterxml.jackson.annotation;
    requires com.fasterxml.jackson.core;
    ...

}

请注意额外的 com.fasterxml.jackson.core 要求,尽管没有这种明确的 maven 依赖性。