Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (867)
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  
  writing xml file to jar location  (Read 1795 times)
0 Members and 1 Guest are viewing this topic.
Offline philfrei
« Posted 2016-08-04 22:57:11 »

Problem: I'm having trouble trying to figure out how to save an xml file to within a jar. It seemed to me that this would be a reasonable form of persistence for some settings data. The file is of xml settings for an audio mix. There are currently two files: day.xml and night.xml for two different mixes.

If these is no way to save to the internals of a jar or if doing so is a bad plan, no need to read further. But a suggestion for an alternative plan would be helpful. I'm wanting to ship the jar for use as a library, and want the owner of that library to be able to save/load settings that they set up. The Java Properties solution was discarded because of the hierarchical complexities and multiple settings docs that will probably be needed down the road.

I have managed to read and write xml files to a user's directory, and I can read xml files from the jar. I specify the read from within the jar as follows:
    URL url = this.getClass().getResource("res/day.xml");
    document = builder.parse(url.toURI().toString());

where builder is a valid DocumentBuilder.

For output, which doesn't work, I first tried the following:

    URL url = this.getClass().getResource("res/day.xml");
    writer = factory.createXMLStreamWriter(new FileOutputStream(new File(url.toURI())));

The error message says that the URI is not heirarchical on the above writer assignment.

I also tried this:
    URL url = this.getClass().getResource("res/day.xml");
    File file = new File(url.getFile());

This gives a FileNotFoundException. The error message:
    file:\C:\Users\Owner\Desktop\vangardaudio.jar!\com\adonax\pfaudio\vangard\res\day.xml (The filename, directory name, or volume label syntax is incorrect)

When inspecting the jar file (by changing its suffix to .zip and drilling down) I can verify that the file exists.

Is there a way to save a file in this manner?

Was looking for a tutorial on our site pertaining to saving settings but came up cold. Is there one that I overlooked? Seem like there probably is one somewhere, since it is a pertinent topic to game making.

music and music apps:
Offline Ecumene

JGO Kernel

Medals: 200
Projects: 4
Exp: 8 years

I did not hit her! I did not!

« Reply #1 - Posted 2016-08-04 23:12:36 »

You should be using
when the resource is bundled as a jar/war or any other single file package for that matter. That's your issue xD


Offline philfrei
« Reply #2 - Posted 2016-08-05 01:39:18 »

Thanks, but getResource() vs getResourceAsStream() is not the issue for me. I've been able to load from jars with both forms. It's the task of saving within the jar that I wanted to solve.

As I look into this further, I am finding advice that says saving to within a jar is not a good nor a common practice. It can only been done with considerable hassle: making and saving a new instance of the jar!

music and music apps:
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Hydroque

JGO Coder

Medals: 25
Exp: 5 years

I'm always inspiring a good time.

« Reply #3 - Posted 2016-08-05 01:55:17 »

You can read from a file in use, but you can never write to it.
Keep a seperate jar or a different extension-named zip using ZipOutputStream.

You think I haven't been monitoring the chat? is a compilation <3
Offline ziozio
« Reply #4 - Posted 2016-08-07 15:52:16 »

The only way is to probably save the file to the temp directory of the file system and then run the jar process from with in java as an external process command
Pages: [1]
  ignore  |  Print  

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

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

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

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

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

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

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

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

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

nelsongames (4708 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!