gpt4 book ai didi

java - Kryo 出现 NoClassDefFoundError

转载 作者:行者123 更新时间:2023-11-30 08:08:27 26 4
gpt4 key购买 nike

我正在寻找 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com