writing xml file to jar location

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.

@tldr:
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.

You should be using [icode]getResourceAsStream(…);[/icode] when the resource is bundled as a jar/war or any other single file package for that matter. That’s your issue xD

Source: http://stackoverflow.com/questions/18055189/why-my-uri-is-not-hierarchical

@Ecumene
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!


http://stackoverflow.com/questions/4056682/how-can-my-java-program-store-files-inside-of-its-jar-file

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.

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