Search code examples
javajarexecutableobjectoutputstream

My executable .jar file won't write a save file


I'm making a game for a school assignment, one of the features is that the game can be saved and loaded. While in eclipse everything worked but after making it an executable jar it won't create the file at the specified location.

I am using this code to get it to save in the folder I want:

see below for full code

Note that the folders, quarto & savefiles, are created but the save file itself isn't.

I'm writing object to a .sav file using this code:

see below for full code

Does this have to do with permissions?


Edit: Ran it in cmd, no exceptions when i tried to save. Added a java.policy file to the folder the jar was in, no difference. I got a previously saved file and put it in the quarto/savefiles map because I wanted to see if it did load correctly (this also uses the user.home to get to the right folder). It loaded correctly. I also searched for the savename.sav to see if it saved it somewhere else, didn't find anything.

Full class:

package quarto;

import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

public class TaskSpelOpslaan extends SpelGegevens{


public static void runSpelOpslaan() {

    String savename = SpelOpslaanScherm.getSaveName();
    String userHome = System.getProperty("user.home") + File.separator + "quarto" + File.separator + "savefiles";
    String locatie = userHome;
    File folder = new File(locatie);
    if (!folder.exists()) {
       folder.mkdirs();
    }

    try{ 
        if(!savename.contains(".sav"))
        {
            savename = (savename+".sav");
        }
        else
        {
            return; 
        }
        FileOutputStream saveFile=new FileOutputStream(folder+File.separator + savename);

        ObjectOutputStream save = new ObjectOutputStream(saveFile);

        save.writeObject(bordInfo);
        save.writeObject(stukGeplaatst);
        save.writeObject(stukGeselecteerd);
        save.writeObject(spelerBeurt);
        save.writeObject(gekozenStuk);
        save.writeObject(bordImage);
        save.writeObject(stukImage);
        save.writeObject(naamSpeler1);
        save.writeObject(naamSpeler2);

        save.close(); 

    }
    catch(Exception exc){
        exc.printStackTrace(); 
    }
}   
}

EDIT2: I'm thankful for all your help, but I just realized a really stupid mistake i made... else{return;} didn't do what I taught it did and could be removed altogether. Sorry for the trouble!

Is there anyway to mark this question closed or should i just let it sit?


Solution

  • Java programs (especially applets) tend to need permissions to do stuff like read and write files. That's why there are .policy files. Add a file called java.policy into the same dirrectory as the jar. In the file you have to grant permissions. So put this into the .policy file:

    grant CodeBase "Example.jar"
    {
        permission java.security.AllPermission;
    };
    

    This will grant all the permissions for Example.jar.