有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

反序列化时引发java ClassNotFoundException

我一直在尝试从bukkit(minecraft)插件中序列化一个向量

这是我要序列化的类:

package net.cerberusstudios.llama.runecraft.neat;

import org.bukkit.util.Vector;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

/**
 * Created by Josiah on 5/11/2015 based on Runix
 */

public class Vector3 extends Vector implements Externalizable {
    private static final long serialVersionUID = 5312566205481472598L;
    public static final Vector3 UP =    new Vector3(0, 1,0);
    public static final Vector3 DOWN =  new Vector3(0,-1,0);
    public static final Vector3 NORTH = new Vector3(0,0,-1);
    public static final Vector3 EAST =  new Vector3( 1,0,0);
    public static final Vector3 SOUTH = new Vector3(0,0, 1);
    public static final Vector3 WEST =  new Vector3(-1,0,0);
    public static final Vector3 NONE =  new Vector3(0,0,0);
    //These map to the minecraft block face, such that you're standing on the side they are pointing
    public static final Vector3[] facing = {DOWN, UP, EAST, WEST, NORTH, SOUTH};//NORTH, SOUTH, WEST, EAST};
    // Runecraft:  0(down) 1(up) 2(east) 3(west) 4(north) 5(south)
    //use like this: WorldXYZ point = point.offset(Vector3.facing[point.face])
    public static final String[] faceString = {"Down", "Up", "East", "West", "North", "South"};
    //Converts a side to the opposite side. This is the same as XOR'ing it with 1.
    public static final int[] oppositeSide = new int[]{1, 0, 3, 2, 5, 4};
    public static final Vector3[] xzRotationOrder = {Vector3.NORTH, Vector3.EAST, Vector3.SOUTH, Vector3.WEST,};
    public static final Vector3[] xyRotationOrder = {Vector3.UP, Vector3.EAST, Vector3.DOWN, Vector3.WEST,};

    public static final Vector3[] yzRotationOrder = {Vector3.NORTH, Vector3.UP, Vector3.SOUTH, Vector3.DOWN,};
    public static final Vector3[] conductanceNeighbors = {
            new Vector3( 0, 1, 0),
            new Vector3( 0,-1, 0),
            new Vector3( 0, 0,-1),
            new Vector3( 1, 0, 0),
            new Vector3( 0, 0, 1),
            new Vector3(-1, 0, 0),

            new Vector3( 1, 1, 0),
            new Vector3(-1, 1, 0),
            new Vector3( 0, 1, 1),
            new Vector3( 0, 1,-1),

            new Vector3( 1, 0, 1),
            new Vector3(-1, 0, 1),
            new Vector3( 1, 0,-1),
            new Vector3(-1, 0,-1),

            new Vector3( 1,-1, 0),
            new Vector3(-1,-1, 0),
            new Vector3( 0,-1, 1),
            new Vector3( 0,-1,-1)};

    public Vector3(int mx, int my, int mz){
        this.x = mx;
        this.y = my;
        this.z = mz;
    }

    /** Returns a difference vector such that reference + vector = destination */
    public Vector3(WorldXYZ reference, WorldXYZ destination){
        x = (int) (destination.getX() - reference.getX());
        y = (int) (destination.getY() - reference.getY());
        z = (int) (destination.getZ() - reference.getZ());
    }

    public Vector3(Vector face)
    {
        x = (int) face.getX();
        y = (int) face.getY();
        z = (int) face.getZ();
    }

    public String toString(){
        return "\"x\":"+x + ", \"y\":" + y + ", \"z\":" + z;
    }

    public Vector3 multiply(int m) {
        return new Vector3(intX()*m, intY()*m, intZ()*m);
    }

    public int intX() { return (int)x; }
    public int intY() { return (int)y; }
    public int intZ() { return (int)z; }

    /**
     * The object implements the writeExternal method to save its contents
     * by calling the methods of DataOutput for its primitive values or
     * calling the writeObject method of ObjectOutput for objects, strings,
     * and arrays.
     *
     * @param out the stream to write the object to
     *
     * @throws IOException Includes any I/O exceptions that may occur
     * @serialData Overriding methods should use this tag to describe
     * the data layout of this Externalizable object.
     * List the sequence of element types and, if possible,
     * relate the element to a public/protected field and/or
     * method of this Externalizable class.
     */
    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeDouble(x);
        out.writeDouble(y);
        out.writeDouble(z);
    }

    /**
     * The object implements the readExternal method to restore its
     * contents by calling the methods of DataInput for primitive
     * types and readObject for objects, strings and arrays.  The
     * readExternal method must read the values in the same sequence
     * and with the same types as were written by writeExternal.
     *
     * @param in the stream to read data from in order to restore the object
     *
     * @throws IOException if I/O errors occur
     * @throws ClassNotFoundException If the class for an object being
     * restored cannot be found.
     */
    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        x = in.readDouble();
        y = in.readDouble();
        z = in.readDouble();
    }
}

我用来测试序列化的代码是:

Object serializeTest = SerializationUtils.clone(new Vector3(1,1,1));

这就是我得到的错误:

[16:48:03] [Server thread/ERROR]: Could not pass event PlayerInteractEvent to Runecraft v3.0
org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:297) ~[craftbukkit-1.8.jar:git-Bukkit-1092acb]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit-1.8.jar:git-Bukkit-1092acb]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit-1.8.jar:git-Bukkit-1092acb]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit-1.8.jar:git-Bukkit-1092acb]
        at org.bukkit.craftbukkit.v1_8_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:209) [craftbukkit-1.8.jar:git-Bukkit-1092acb]
        at net.minecraft.server.v1_8_R1.PlayerInteractManager.interact(PlayerInteractManager.java:462) [craftbukkit-1.8.jar:git-Bukkit-1092acb]
        at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:693) [craftbukkit-1.8.jar:git-Bukkit-1092acb]
        at net.minecraft.server.v1_8_R1.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:50) [craftbukkit-1.8.jar:git-Bukkit-1092acb]
        at net.minecraft.server.v1_8_R1.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:80) [craftbukkit-1.8.jar:git-Bukkit-1092acb]
        at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [craftbukkit-1.8.jar:git-Bukkit-1092acb]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_25]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_25]
        at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:643) [craftbukkit-1.8.jar:git-Bukkit-1092acb]
        at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:284) [craftbukkit-1.8.jar:git-Bukkit-1092acb]
        at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:598) [craftbukkit-1.8.jar:git-Bukkit-1092acb]
        at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:506) [craftbukkit-1.8.jar:git-Bukkit-1092acb]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_25]
Caused by: org.apache.commons.lang.SerializationException: java.lang.ClassNotFoundException: net.cerberusstudios.llama.runecraft.neat.Vector3
        at org.apache.commons.lang.SerializationUtils.deserialize(SerializationUtils.java:166) ~[craftbukkit-1.8.jar:git-Bukkit-1092acb]
        at org.apache.commons.lang.SerializationUtils.deserialize(SerializationUtils.java:193) ~[craftbukkit-1.8.jar:git-Bukkit-1092acb]
        at net.cerberusstudios.llama.runecraft.Runecraft_MAIN.exportFaith(Runecraft_MAIN.java:7119) ~[?:?]
        at net.cerberusstudios.llama.runecraft.Runecraft_MAIN.masterRuneFunctionExecute(Runecraft_MAIN.java:5383) ~[?:?]
        at net.cerberusstudios.llama.runecraft.Runecraft_MAIN.onRightClick(Runecraft_MAIN.java:442) ~[?:?]
        at net.cerberusstudios.llama.runecraft.Runecraft.onPlayerInteract(Runecraft.java:367) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_25]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_25]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_25]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_25]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:295) ~[craftbukkit-1.8.jar:git-Bukkit-1092acb]
        ... 16 more
Caused by: java.lang.ClassNotFoundException: net.cerberusstudios.llama.runecraft.neat.Vector3
        at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_25]
        at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_25]
        at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_25]
        at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_25]
        at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_25]
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) ~[?:1.8.0_25]
        at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_25]
        at java.lang.Class.forName0(Native Method) ~[?:1.8.0_25]
        at java.lang.Class.forName(Unknown Source) ~[?:1.8.0_25]
        at java.io.ObjectInputStream.resolveClass(Unknown Source) ~[?:1.8.0_25]
        at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source) ~[?:1.8.0_25]
        at java.io.ObjectInputStream.readClassDesc(Unknown Source) ~[?:1.8.0_25]
        at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source) ~[?:1.8.0_25]
        at java.io.ObjectInputStream.readObject0(Unknown Source) ~[?:1.8.0_25]
        at java.io.ObjectInputStream.readObject(Unknown Source) ~[?:1.8.0_25]
        at org.apache.commons.lang.SerializationUtils.deserialize(SerializationUtils.java:163) ~[craftbukkit-1.8.jar:git-Bukkit-1092acb]
        at org.apache.commons.lang.SerializationUtils.deserialize(SerializationUtils.java:193) ~[craftbukkit-1.8.jar:git-Bukkit-1092acb]
        at net.cerberusstudios.llama.runecraft.Runecraft_MAIN.exportFaith(Runecraft_MAIN.java:7119) ~[?:?]
        at net.cerberusstudios.llama.runecraft.Runecraft_MAIN.masterRuneFunctionExecute(Runecraft_MAIN.java:5383) ~[?:?]
        at net.cerberusstudios.llama.runecraft.Runecraft_MAIN.onRightClick(Runecraft_MAIN.java:442) ~[?:?]
        at net.cerberusstudios.llama.runecraft.Runecraft.onPlayerInteract(Runecraft.java:367) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_25]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_25]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_25]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_25]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:295) ~[craftbukkit-1.8.jar:git-Bukkit-1092acb]
        ... 16 more

我一直在四处寻找,似乎it找不到该类,因为它们没有在类路径中引用,我认为这并没有多大意义,因为它们来自我正在进行的项目

我也在考虑将其转换为JSON而不是序列化,这是一种更好的方法吗


共 (1) 个答案

  1. # 1 楼答案

    老实说,在我看来,一切都比输出原始对象要好。 我使用XML来使用库XStream序列化我的大多数对象

        XStream xstream = new XStream();
        System.out.println(xstream.toXML(new Vector3(x,y,z)));