0

I have a large number of user settings that I want to store within an XML file. If it is possible, I would like to keep that XML file within the JAR itself, but so far I have found nothing that permits me to do that (lots of searching on Google turned up nothing).

I plan to use JAXB to handle reading and writing the XML file but first I need to know if it's possible to write to the XML file within the JAR during run time.

So far getClass().getResourceAsStream() seems to be on target except it only returns an input stream to read from the file. Is it possible at all to write to a file stored as a resource within a JAR file at run time? To open that file for Output?

Will
  • 3,413
  • 7
  • 50
  • 107
  • No, actually more precisely, not without more work then it's worth. Remember, Jar files are just zipped files and it's a complete pain in the back to add files to them and you may not be able to rename/delete a jar while it's running on some systems... – MadProgrammer Aug 16 '14 at 00:15

1 Answers1

1

The Java process will lock dependencies on the classpath. You won't be able to change your JAR because the OS will tell you that it is in use.

java -cp "your_jar.jar;your_jar2.jar" YourMainClass

Only way I can think of is to load your JAR at runtime, so you can change a second jar that would be part of your application.

I (kinda) understand why you are doing it, but don't do it. Keep configuration out of your JAR.

Community
  • 1
  • 1
Evandro Pomatti
  • 13,341
  • 16
  • 97
  • 165