当前位置: 首页 > 知识库问答 >
问题:

Kryonet序列化错误

顾磊
2023-03-14

在任何人试图告诉我这是一个重复之前,我已经试过了关于这个的解决方案,以及几乎所有其他我能找到的答案。

我的项目在IntelliJ中构建得很好,通过了我设置的所有测试,它甚至在IntelliJ内部运行得也很好,当我试图运行maven构建的JAR时,除了几个例外,我就被打了个耳光。

代码所指向的类:

package com.albion;

import com.albion.logging.Loggable;
import com.albion.packets.Packet;
import com.albion.packets.Packets;
import com.esotericsoftware.kryonet.Server;
import com.esotericsoftware.minlog.Log;

import javax.swing.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;
import java.util.logging.Level;

public class Main extends Loggable {
    protected static Integer PORT = 47532;

    protected static byte[] key = { /* contents omitted for obvious reasons */ };

    protected static Server server;
    protected static Controller controller;

    public static void main(String[] args) throws IOException {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        }

        Log.set(Log.LEVEL_DEBUG);
        server = new Server();
        server.bind(PORT);

        try {
            controller = new Controller();
            controller.setVisible(true);
            ClientManager.setController(controller);
        } catch (Exception ex) {
            getLog().log(Level.SEVERE, "Error creating a new Controller instance: ", ex.getCause());
        }

        handlers();
        register();

        server.start();
    }

    private static void handlers() {
        getLog().info("Creating new PacketHandler");
        PacketHandler handler = new PacketHandler(controller);

        getLog().info("Creating new ClientListener");
        ClientListener listener = new ClientListener(handler);

        getLog().info("Adding listeners to server");
        server.addListener(listener);
    }

    private static void register() {

        getLog().info("Registering data types");

        server.getKryo().register(UUID.class);
        server.getKryo().register(byte[].class);
        server.getKryo().register(Packet.class);
        server.getKryo().register(String.class);
        server.getKryo().register(HashMap.class);
        server.getKryo().register(Packets.class);
        server.getKryo().register(String[].class);

    }
}

根据我看到的例外情况,当通过命令提示符运行时,问题出现在第31行,它将是:

    server = new Server();

命令提示符中的堆栈跟踪如下所示:

    Exception in thread "main" java.lang.IllegalArgumentException: Unable to create serializer "com.esotericsoftware.kryo.serializers.FieldSerializer" for class: com.esotericsoftware.kryonet.FrameworkMessage$RegisterTCP
    at com.esotericsoftware.kryo.factories.ReflectionSerializerFactory.makeSerializer(ReflectionSerializerFactory.java:48)
    at com.esotericsoftware.kryo.factories.ReflectionSerializerFactory.makeSerializer(ReflectionSerializerFactory.java:26)
    at com.esotericsoftware.kryo.Kryo.newDefaultSerializer(Kryo.java:351)
    at com.esotericsoftware.kryo.Kryo.getDefaultSerializer(Kryo.java:344)
    at com.esotericsoftware.kryo.Kryo.register(Kryo.java:365)
    at com.esotericsoftware.kryonet.KryoSerialization.<init>(KryoSerialization.java:31)
    at com.esotericsoftware.kryonet.KryoSerialization.<init>(KryoSerialization.java:23)
    at com.esotericsoftware.kryonet.Server.<init>(Server.java:91)
    at com.esotericsoftware.kryonet.Server.<init>(Server.java:73)
    at com.albion.Main.main(Main.java:31)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.esotericsoftware.kryo.factories.ReflectionSerializerFactory.makeSerializer(ReflectionSerializerFactory.java:35)
    ... 9 more
Caused by: java.lang.RuntimeException: Cannot create UnsafeFieldFactory
    at com.esotericsoftware.kryo.serializers.FieldSerializer.getUnsafeFieldFactory(FieldSerializer.java:408)
    at com.esotericsoftware.kryo.serializers.FieldSerializer.newMatchingCachedField(FieldSerializer.java:375)
    at com.esotericsoftware.kryo.serializers.FieldSerializer.newCachedField(FieldSerializer.java:343)
    at com.esotericsoftware.kryo.serializers.FieldSerializer.createCachedFields(FieldSerializer.java:307)
    at com.esotericsoftware.kryo.serializers.FieldSerializer.rebuildCachedFields(FieldSerializer.java:239)
    at com.esotericsoftware.kryo.serializers.FieldSerializer.rebuildCachedFields(FieldSerializer.java:156)
    at com.esotericsoftware.kryo.serializers.FieldSerializer.<init>(FieldSerializer.java:133)
    ... 14 more
Caused by: java.lang.ClassNotFoundException: com.esotericsoftware.kryo.serializers.UnsafeCachedFieldFactory
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at com.esotericsoftware.kryo.serializers.FieldSerializer.getUnsafeFieldFactory(FieldSerializer.java:405)
    ... 20 more
<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>
<parent>
    <groupId>com.albion</groupId>
    <artifactId>Server</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>
<artifactId>Server</artifactId>
<dependencies>
    <dependency>
        <groupId>com.albion</groupId>
        <artifactId>ServerCommons</artifactId>
        <version>1.0-SNAPSHOT</version>
        <scope>compile</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <minimizeJar>true</minimizeJar>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <archive>
                    <index>true</index>
                    <manifest>
                        <addClasspath>true</addClasspath>
                    </manifest>
                    <manifestEntries>
                        <Main-Class>com.albion.Main</Main-Class>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

共有1个答案

干茂才
2023-03-14

这似乎是Maven的shade插件中的minimizeJar造成的。在最后一次尝试中删除了这个选项,然后一切都像发条一样正常运转。

 类似资料:
  • 我正在尝试使用网络库kryonet创建一个多人游戏,我得到了连接和发送字符串的工作,但现在我正在尝试发送对象。我想做的是发送一个数组列表,但它给了我这个错误。我也尝试只发送一个对象,它给了我同样的错误。 我尝试在Metor类中创建一个没有参数的构造函数,但这也不起作用 编辑:所以我得出的结论是,Kryonet在序列化slick2d图像类时有问题,或者与Kryonet和slick2d有不同的冲突。

  • KryoNet这个Java类包提供了一套简洁的API来实现高效的,基于TCP和UDP协议的客户端/服务器NIO网络通信。KryoNet可以运行于桌面应用程序和Android平台中。由于KryoNet非常高效,所以特别适用于开发游戏应用程序和进程间通信。 示例代码: Server server = new Server();server.start();server.bind(54555, 5477

  • 问题内容: 我正在尝试使用protobuf序列化结构。经过许多小时试图弄清楚我在做什么错,我决定测试google的示例,但效果不佳 我从Google(https://developers.google.com/protocol- buffers/docs/javatutorial )获得以下协议: 我正在尝试将其序列化: byte []序列化= john.toByteArray(); 我得到“ j

  • ClassCastException:com.google.gson.internal.LinkedTreeMap不能强制转换为java.util.HashMap 提前谢了。

  • 问题内容: 我试图反序列化以DateTime作为修饰符的类: 但是,当我尝试tro反序列化时,却遇到以下异常: 我用它来反序列化: 还有我的jsonData的示例: 问题答案: 期望使用无参数构造函数。的最新版本没有这样的构造函数。 如果您已固定格式,即。应该只是一个时间戳,那么你可以简单地注册与。它将在内部用于字段。您可以摆脱注释。 您需要添加库。

  • (我之前的问题/议题不够具体,因此完全重写了。) 问题:ServiceStack 4.0.16不适用于Redis。 重新创建的步骤: 在VS 2013中创建一个新的ASP. NET Web表单应用程序。 更新Nuget包 安装Nuget包= 容器登记册(c)= > 运行项目,你应该只得到一个空白屏幕在Chrome 导航到/元数据,你会得到一个通用的HTTP错误500.0-内部服务器错误 打开事件查