Kryo的NoClassDefFoundError

时间:2015-06-10 09:32:00

标签: java serialization classpath kryo

我正在寻找Kryo自定义序列化和De序列化示例。 如何检查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());
        }
}

}

1 个答案:

答案 0 :(得分:1)

感谢您的回复。 我以某种方式设法纠正了异常。在将“objenesis-1.2.jar”包含到构建路径中之后,代码工作正常。