4

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?

East
  • 43
  • 1
  • 6
  • 3
    It could have something to do with permissions. Any error messages when you run your code? Also, we probably need to see a bit more code. The savename field is valid, I assume? – MarsAtomic Jul 02 '14 at 19:47
  • 1
    I'd verify that the expression `folder+File.separator+savename` in the second code block was producing the string you expect. If you saw no exception then the file is probably being created, just not where you expect. – Jim Garrison Jul 02 '14 at 20:19
  • Keep in mind that "user.home" might be different in a jar. I suggest you output `System.out.println(System.getProperty("user.home"))` somewhere – MyPasswordIsLasercats Jul 03 '14 at 08:18
  • You must have got an exception which you are ignoring somewhere. Check and report. – user207421 Jul 03 '14 at 08:23

1 Answers1

0

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.

Hullu2000
  • 261
  • 4
  • 19
  • I added the file to the folder the .jar was located. No changes when I try to save. Btw, is that last ; supposed to be there? – East Jul 03 '14 at 11:48
  • As far as i know yes. I rarely do this, I've only done this once and it worked for me. You can also configure java from the controlpanel if you're in Windows. – Hullu2000 Jul 03 '14 at 12:19