java - Kryo 出现 NoClassDefFoundError

标签 java serialization classpath kryo

我正在寻找 Kryo 自定义序列化和反序列化示例。 如何检查Kryo读写函数的正确性。

我写了一些代码来检查,但它返回异常。任何帮助将不胜感激。 提前致谢。

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoSerializable;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.MapSerializer;

public class KryoSample implements KryoSerializable{


int number = 12345;
String name = "Siemens";
boolean good = false;

String type = "Serializer ";
public void someValues(){       
    String name = " Kryo ";
    String type = "Serializer ";
}

public KryoSample(){}

public KryoSample(String name, String type){

     name = " Kryo ";
     type = "Serializer ";

}


@Override
public void read(Kryo kryo, Input input) {
    // TODO Auto-generated method stub      
    try{

   String name  = input.readString();
   int number = input.readInt();
   boolean good = input.readBoolean();

   System.out.println(name+": "+number+" : "+good);

    }catch(Exception e){
        System.out.println(" Read Exception  "+e.getMessage());
    }       
}


@Override
public void write(Kryo kryo, Output output) {
    // TODO Auto-generated method stub      
    try{
    output.writeString(name);
    output.writeBoolean(good);
    output.write(number);
    }catch(Exception e){
        System.out.println("  Write Exception "+e.getMessage());
    }

}
public static void main(String args[]){
    try{
    Kryo kryoObj = null;
    //kryoObj = new Kryo();
    kryoObj.setReferences(false);
    kryoObj.register(KryoSample.class, new MapSerializer());

    System.out.println(" TRY: ");

    //Kryo kryoObj = new Kryo();
    Output outputObj = new Output();        
    Input inputObj = new Input();       
    KryoSample kryoSample = new KryoSample();

    kryoSample.write(kryoObj, outputObj);
    kryoSample.read(kryoObj, inputObj);
    }catch(Exception e){
        System.out.println("Kryo Exeption "+e.getMessage());
        }
}

}

最佳答案

感谢您的回复。 我设法纠正了这个异常。将“objenesis-1.2.jar”包含到构建路径后,代码可以正常工作。

关于java - Kryo 出现 NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30752483/

相关文章:

java - 使用 getName() 时如何摆脱文件类型?

java - Hadoop MapReduce 小文件内存不足

php - 将数据保存为数组或单个字段 (php/MySQL)

java - Eclipse 类路径中的 combineaccessrules 是什么意思?

java - "NoDefClassNotFound"和 "T cannot not be resolved to a type",在运行时

java - Apache Ivy : XML Namespace and Imported Properties

java - 玩吗!框架有任何内置机制来防止 session 劫持吗?

c# - 具有多态子对象的类型的 Json.Net 序列化

php - MySQL - 只选择包含序列化数据的单元格

找不到 Java 类