NoClassDefFoundError with Kryo

7.4k views Asked by At

I am looking for Kryo custom Serialization and De serialization example. How to check the the correctness of the Kryo read and write function.

I have written some code to check, but it return exception. Any help would be appreciable. Thanks in advance.

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

There are 1 answers

0
BusyBee On BEST ANSWER

Thanks for the responses. I somehow managed to correct the exception. After including "objenesis-1.2.jar" into the build path the code works fine.