NoClassDefFoundError - 反序列化器$ Base

时间:2014-01-31 20:51:35

标签: java json scala maven playframework

当运行调用Scala的Java代码,即Play库时,我在运行下面的测试代码时看到了这个错误。

java.lang.NoClassDefFoundError: org/codehaus/jackson/map/Deserializers$Base

我查找了Deserializers$Base,找到了out这个包的一部分org.codehaus.jackson。然后,我看到此packagejackson-mapper-asl artifactId的一部分。

爪哇

// Main
Foo foo = new Foo();
foo.do();  // <-- top of stack trace

public class Foo {
     public String do() {
        List<JsValue> jsonList = new ArrayList<JsValue>();
        // populate jsonList
        return JsonUtil.build(json); // <-- second line of stack trace
     }

Scala的

object JsonUtil {
import collection.JavaConverters._
   def build(x: java.util.List[JsValue]): String = 
       Json.obj("bar" -> x.asScala.toList).toString
   }
}

将此依赖项添加到我的POM后,我仍然遇到上述错误。完整堆栈跟踪:

java.lang.NoClassDefFoundError: org/codehaus/jackson/map/Deserializers$Base
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at play.api.libs.json.JacksonJson$module$.setupModule(JsValue.scala:460)
    at org.codehaus.jackson.map.ObjectMapper.registerModule(ObjectMapper.java:444)
    at play.api.libs.json.JacksonJson$.<init>(JsValue.scala:464)
    at play.api.libs.json.JacksonJson$.<clinit>(JsValue.scala)
    at play.api.libs.json.Json$.stringify(Json.scala:48)
    at play.api.libs.json.JsValue$class.toString(JsValue.scala:79)
    at play.api.libs.json.JsObject.toString(JsValue.scala:158)
    at com.workspace.json.JsonUtil$.build(JsonUtil.scala:95)

请注意,我已使用play_2.10-2.1.5安装了play-iteratees_2.10-2.1.5mvn install ...个库。另外,我添加了正确的Jackson依赖项,但上述错误再次出现在运行时。

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>

1 个答案:

答案 0 :(得分:0)

我认为你错过了slf4j-api(这就是我用mvn编译给我编译的问题)。此外,您不需要安装该版本,只需将所有需要的内容添加到您的pom中。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>stackoverflow</groupId>
    <artifactId>21489814</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <dependency.jackson-mapper-asl.version>1.9.13</dependency.jackson-mapper-asl.version>
        <dependency.play-java_2.10.version>2.1.5</dependency.play-java_2.10.version>
        <dependency.play-iteratees_2.10.version>2.1.5</dependency.play-iteratees_2.10.version>
        <dependency.slf4j.version>1.7.5</dependency.slf4j.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
        </dependency>
        <dependency>
            <groupId>play</groupId>
            <artifactId>play-java_2.10</artifactId>
        </dependency>
        <dependency>
            <groupId>play</groupId>
            <artifactId>play-iteratees_2.10</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.codehaus.jackson</groupId>
                <artifactId>jackson-mapper-asl</artifactId>
                <version>${dependency.jackson-mapper-asl.version}</version>
            </dependency>
            <dependency>
                <groupId>play</groupId>
                <artifactId>play-java_2.10</artifactId>
                <version>${dependency.play-java_2.10.version}</version>
            </dependency>
            <dependency>
                <groupId>play</groupId>
                <artifactId>play-iteratees_2.10</artifactId>
                <version>${dependency.play-iteratees_2.10.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${dependency.slf4j.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <repositories>
        <repository>
            <id>TypesafeRepository</id>
            <name>Typesafe Repository</name>
            <url>http://repo.typesafe.com/typesafe/releases/</url>
            <layout>default</layout>
        </repository>
    </repositories>
</project>

我希望这会有所帮助。