Persistent Data in a fat jar

Hi, guys!

I created a fat jar for my game and I use JSON to save and load data.

When the app runs from Netbeans, everthing works fine.

But when I run the app from the fat jar generated, I can’t save data anymore. I still can read and load, but can’t save.

The code is inside a try/catch and I got this exception:

java.io.FileNotFoundException: data\savegame.json (The system cannot find the path specified)

The “funny” thing is that actually it can find the path, since it loads data from the same file. I’ve checked already that the files are not “Read only”.

Someone knows what is happening?

Writing to a jar file is not the way to go, create a “data” folder alongside your jar to which you can then write:

Stackoverflow: “Loading and saving resources inside a runnable jar? [duplicate]”

Hello, VaTTeRGeR!

I just extracted the data folder from the jar and let it at the same folder, like:
/mygame.jar
/data/config.json

And now it works fine! Thanks!

Depending on the installation location of your project, you should probably consider a common user data path for saving your data. Saving to a folder right beside your jar might be blocked on some OSes.

Take a look at https://github.com/harawata/appdirs if that becomes a problem.

If you don’t want to use an external library


File saveGameDir = new File(FileSystemView.getFileSystemView().getDefaultDirectory(), "My Fancy Game Title/Save Games")

might also be an option.

Sometimes I have to remind myself that a jar is basically a zip file, not a file folder. URL’s can be used to read the contents when File commands fail to see inside. My recollection when looking into this before is that writing inside of a jar is theoretically possible but so troublesome and complicated that it is pretty much never done.

Java gives you access to some standard file locations via System Properties. I have done the thing of setting up a file folder specific to the application within the address given by the following code:

		String fileLoc = System.getProperty("user.home");

and then making use of that folder for persistent storage. In Windows, the user.home is the folder that holds common folders like “Desktop” and “Documents” for a specific User.

Hi, guys! Thanks for all!

Another option I’m thinking: if I unzip the jar and create a *.bat file, it could work?

Why tho? Are you trying to weasel your way around proper file handling?

The techniques philfrei and cylab described are best practice and will save you lots of headache, it’s even better than what i advised you to do.

I read some about and actually it’s not well advised to use a Fat Jar. For now, I’m using, but in the future I most likely will not use a fat jar.

You mean you were also packing all assets and libraries into your jar up until now?
I thought you were talking exclusively about config files and savegames, in this case i totally agree with not packing it all into the jar.

Oh, not only the json files, it’s all the data. It’s a really fat jar, lol