我正在尝试测试java应用程序(有主要方法)。我正在使用maven将依赖项添加到我的项目中。我直接从IntelliJ IDE运行应用程序,而不是创建一个胖jar然后运行。但不知何故,我无法在运行时找到JSON类并获得异常
Exception in thread "main"
java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper
at com.hospitality.component.visma.websocket.Test.<init>(Test.java:10)
at com.hospitality.component.visma.websocket.Test.main(Test.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ObjectMapper
这是我的pom.xml:
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
</dependency>
这个项目有一个父pom.xml文件,它已经包含了这个jar.mvn dependecy:树,显示了以下jar列表:
[INFO] Building visma-websocket-server 0.0.1-SNAPSHOT
[INFO] -----------------------------------------------------------
-------------
[INFO]
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ visma-websocket-server ---
[INFO] com.hospitality.hp.components:visma-websocket-server:jar:0.0.1-SNAPSHOT
[INFO] +- org.glassfish.metro:webservices-rt:jar:2.3:provided
[INFO] | \- org.glassfish.metro:webservices-api:jar:2.3:provided
[INFO] | \- javax.annotation:javax.annotation-api:jar:1.2-b03:provided
[INFO] +- junit:junit:jar:4.11:test
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- org.hamcrest:hamcrest-all:jar:1.3:test
[INFO] +- javax:javaee-api:jar:7.0:provided
[INFO] | \- com.sun.mail:javax.mail:jar:1.5.0:provided
[INFO] | \- javax.activation:activation:jar:1.1:provided
[INFO] +- org.slf4j:slf4j-api:jar:1.7.21:compile
[INFO] +- ch.qos.logback:logback-classic:jar:1.1.7:compile
[INFO] | \- ch.qos.logback:logback-core:jar:1.1.7:compile
[INFO] +- org.eclipse.jetty.websocket:websocket-server:jar:9.0.3.v20130506:compile
[INFO] | +- org.eclipse.jetty.websocket:websocket-common:jar:9.0.3.v20130506:compile
[INFO] | | +- org.eclipse.jetty.websocket:websocket-api:jar:9.0.3.v20130506:compile
[INFO] | | \- org.eclipse.jetty:jetty-util:jar:9.0.3.v20130506:compile
[INFO] | +- org.eclipse.jetty.websocket:websocket-servlet:jar:9.0.3.v20130506:compile
[INFO] | \- org.eclipse.jetty:jetty-http:jar:9.0.3.v20130506:compile
[INFO] +- com.hospitality.hp.components:jndi-tools:jar:0.0.1-SNAPSHOT:compile
[INFO] +- org.eclipse.jetty:jetty-server:jar:9.0.3.v20130506:compile
[INFO] | +- org.eclipse.jetty.orbit:javax.servlet:jar:3.0.0.v201112011016:compile
[INFO] | \- org.eclipse.jetty:jetty-io:jar:9.0.3.v20130506:compile
[INFO] +- com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:jar:2.2.1:provided
[INFO] | +- com.fasterxml.jackson.jaxrs:jackson-jaxrs-base:jar:2.2.1:provided
[INFO] | +- com.fasterxml.jackson.core:jackson-core:jar:2.2.1:provided
[INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.2.1:provided
[INFO] | | \- com.fasterxml.jackson.core:jackson-annotations:jar:2.2.1:provided
[INFO] | \- com.fasterxml.jackson.module:jackson-module-jaxb-annotations:jar:2.2.1:provided
[INFO] \- org.projectlombok:lombok:jar:1.16.8:provided
有人可以告诉我如何解决这个问题,因为默认范围是编译,所以当我在IDE中运行我的应用程序时,为什么这不可用?但是在JunitTest中没有任何问题。知道如何解决这个问题吗?
------------------------- dependencyManagement --------------------- ------------
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
</dependencyManagement>
答案 0 :(得分:0)
| +- com.fasterxml.jackson.core:jackson-databind:jar:2.2.1:provided
范围为provided
,因此NoClassDefFoundError
是正常的;应该省略范围说明并使用默认值。