Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (868)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Persistent Data in a fat jar  (Read 2115 times)
0 Members and 1 Guest are viewing this topic.
Offline fenaryh
« Posted 2018-12-22 17:18:34 »

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: 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?
Online VaTTeRGeR
« Reply #1 - Posted 2018-12-22 23:10:42 »

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]"
Offline fenaryh
« Reply #2 - Posted 2018-12-23 00:32:02 »

Hello, VaTTeRGeR!

I just extracted the data folder from the jar and let it at the same folder, like:

And now it works fine! Thanks!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cylab

JGO Kernel

Medals: 195

« Reply #3 - Posted 2018-12-24 09:10:40 »

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 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.

Mathias - I Know What [you] Did Last Summer!
Offline philfrei
« Reply #4 - Posted 2018-12-24 09:45:23 »

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.

music and music apps:
Offline fenaryh
« Reply #5 - Posted 2018-12-24 13:24:48 »

Hi, guys! Thanks for all!

Another option I'm thinking: if I unzip the jar and create a *.bat file, it could work?
Online VaTTeRGeR
« Reply #6 - Posted 2018-12-24 15:02:48 »

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.
Offline fenaryh
« Reply #7 - Posted 2018-12-24 18:30:03 »

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.
Online VaTTeRGeR
« Reply #8 - Posted 2018-12-24 19:36:05 »

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.
Offline fenaryh
« Reply #9 - Posted 2018-12-25 23:03:45 »

Oh, not only the json files, it's all the data. It's a really fat jar, lol
Pages: [1]
  ignore  |  Print  

Riven (579 views)
2019-09-04 15:33:17

hadezbladez (5504 views)
2018-11-16 13:46:03

hadezbladez (2398 views)
2018-11-16 13:41:33

hadezbladez (5764 views)
2018-11-16 13:35:35

hadezbladez (1220 views)
2018-11-16 13:32:03

EgonOlsen (4660 views)
2018-06-10 19:43:48

EgonOlsen (5681 views)
2018-06-10 19:43:44

EgonOlsen (3196 views)
2018-06-10 19:43:20

DesertCoockie (4094 views)
2018-05-13 18:23:11

nelsongames (5114 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!