Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (780)
Games in Android Showcase (233)
games submitted by our members
Games in WIP (857)
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 888 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:

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?
Offline 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:
/mygame.jar
/data/config.json

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: 188



« 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 https://github.com/harawata/appdirs if that becomes a problem.

If you don't want to use an external library
1  
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:
1  
      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: http://adonax.com
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?
Offline 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.
Offline 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  
 
 

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

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

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

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

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

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

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

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

nelsongames (1945 views)
2018-04-24 18:15:36

nelsongames (2625 views)
2018-04-24 18:14:32
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

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org 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‑gaming.org
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!