My .jar executable will not write save file

I am making a game for school, one of the features is that the game can be saved and loaded. While everything worked in eclipse, but after creating an executable banner, it did not create a file in the specified location.

I use this code to save it in the folder I want:

see below for full code

Note that folders, quarto and savefiles are created, but the save file itself is missing.

I am writing an object to a .sav file using this code:

see below for full code

Is it related to permissions?


Edit: Ran it in cmd, no exceptions when I tried to save. The java.policy file was added to the folder in which the jar was located, no difference. I got the previously saved file and put it in the quarto / savefiles card because I wanted to see if it loaded correctly (this also uses user.home to get to the right folder). It is loaded correctly. I also looked for savename.sav to see if it saved it elsewhere, found nothing.

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 am grateful for your help, but I realized that the really stupid mistake I made ... else {return;} did not do what I taught him, and could even be deleted. Excuse for troubling!

In any case, to close this question, or will I just let him sit?

+4
1

Java- ( ), , , . .policy. java.policy , . . .policy:

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

Example.jar.

0

Source: https://habr.com/ru/post/1546912/


All Articles