title: Java - NotSerializableException (saving an object)
body: <p>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:</p>
  
  <blockquote>
    <p>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)</p>
  </blockquote>
  
  <p>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:</p>
  
  <pre><code>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&lt;Player&gt; 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&lt;Player&gt;();
  }
  //and so on.....
  </code></pre>
  
  <p>Now, here is the Code snipet where I save the track object:</p>
  
  <pre><code>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) {}
      }
  }
  </code></pre>
  
  <p>So like i said Java creates the object so it kinda works. But I have no idea how to get rid of the warning.</p>
  
  <p>Thanks for your help</p>