0

So today I am working on a plugin for a Minecraft Server. For this, i created a "Track" class which represents a racing track. In order to storage all existing tracks with their data I want save the whole track object which makes everything easier for me. In conclusion, I implemented Serializable to my Track class to use the ObjectOutputStream later. When I start saving the tracks Java creates a file of the object but it gives me a warning/error as well:

14:19:00] [Server thread/WARN]: java.io.NotSerializableException: org.bukkit.craftbukkit.v1_8_R2.CraftWorld [14:19:00] [Server thread/WARN]: at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184) [14:19:00] [Server thread/WARN]: at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548) [14:19:00] [Server thread/WARN]: at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509) [14:19:00] [Server thread/WARN]: at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432) [14:19:00] [Server thread/WARN]: at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178) [14:19:00] [Server thread/WARN]: at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348) [14:19:00] [Server thread/WARN]: at de.avarion.speedrunners.Main.saveTracks(Main.java:206) [14:19:00] [Server thread/WARN]: at de.avarion.speedrunners.Main.onDisable(Main.java:48) [14:19:00] [Server thread/WARN]: at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:323) [14:19:00] [Server thread/WARN]: at org.bukkit.plugin.java.JavaPluginLoader.disablePlugin(JavaPluginLoader.java:359) [14:19:00] [Server thread/WARN]: at org.bukkit.plugin.SimplePluginManager.disablePlugin(SimplePluginManager.java:424) [14:19:00] [Server thread/WARN]: at org.bukkit.plugin.SimplePluginManager.disablePlugins(SimplePluginManager.java:417) [14:19:00] [Server thread/WARN]: at org.bukkit.craftbukkit.v1_8_R2.CraftServer.disablePlugins(CraftServer.java:335) [14:19:00] [Server thread/WARN]: at net.minecraft.server.v1_8_R2.MinecraftServer.stop(MinecraftServer.java:458) [14:19:00] [Server thread/WARN]: at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:590) [14:19:00] [Server thread/WARN]: at java.lang.Thread.run(Thread.java:745)

Here is a part of my Track class. I noticed that eclipse is warning me that the track class does not declare a final static serialVersionUID field. I tried to implement the default and generated serialVersionUID but i didn't help:

import java.io.Serializable;
import java.util.ArrayList;

import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;

public class Track implements Serializable {

private int playerSize;

private String name;
private World world;
private ArrayList<Player> players;
private Location spawn;

private Sign sign;
private Material finishBlocks;
private Material speedBlocks;

public Track(World world, String name, int playerSize) {
    this.world = world;
    this.name = name; 
    this.playerSize = playerSize;
    this.players = new ArrayList<Player>();
}
//and so on.....

Now, here is the Code snipet where I save the track object:

private void saveTracks() {
    FileOutputStream outputStream = null;
    ObjectOutputStream objectOutput = null;
    try {
        File directory = new File("tracks");
        if(!directory.exists()) directory.mkdir();
        for(Track track : tracks) {
            outputStream = new FileOutputStream("tracks/" + track.getName() + ".ser");
            objectOutput = new ObjectOutputStream(outputStream);
            objectOutput.writeObject(track);
        }
    }
    catch (IOException e) {
          e.printStackTrace();
    }
    finally {
          if (objectOutput != null) try { objectOutput.close(); } catch (IOException e) {}
          if (outputStream != null) try { outputStream.close(); } catch (IOException e) {}
    }
}

So like i said Java creates the object so it kinda works. But I have no idea how to get rid of the warning.

Thanks for your help

1 Answer 1

1

This exception throws because of some instance fields are not serialized. So Please check all the instance variables . If some instance variables are not required to serialize then please add transient to that variables.

As per java doc :

Thrown when an instance is required to have a Serializable interface. The serialization runtime or the class of the instance can throw this exception. The argument should be the name of the class.

Sign up to request clarification or add additional context in comments.

4 Comments

@VGR Please read before comment . I asked to put transient which are the fields not required.
Ok so i found out that the error is caused by the fields which are from an api and they are not searialized. So the Problem would be to searialize them, because i don't want to bequeath every class
@AvarionDE is this post helped you
Sry it didn't help. Like I said I am using classes from an external api and they haven't implemented Serializable so I it is not possible to save my track object including those fields

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.