Self-development and overwriting of jar file at runtime

I am currently working on a program that creates diagrams for classes in the teacher class. I put all the data files in the jar. They are read and placed in a table. After starting the main function of the program, it updates the files so that they match the values ​​of the tables. I know that I need to blow up the jar and then rephrase it during extraction to edit the files, but I can not find any explanation on how to repaint during extraction. Does anyone have any ideas?

+4
source share
2 answers

Short answer:

Place the data files outside the binary and send them along with the JAR in a separate folder.

Long:

It looks like you are approaching a problem from the wrong direction. A JAR file is a bit of an executable file ( .exe ) on the Windows platform β€” a read-only binary file containing code .

You can (although this is bad practice) put some resources, such as data files, multimedia, etc. inside a JAR (e.g. inside .exe ). But the best solution would be to place these resources outside the binary so you can switch them without recompiling / rebuilding.

If you need to change resources on the fly while the application is running, you basically have no choice. Data files must be outside the binary. Once again, you will never see a Windows .exe that will change itself.

+4
source

Tomas is right that the following is bad practice, but it is possible.

The contents of the class path are read into memory at boot time, however the files are mutable, but their changes will not be displayed after initialization. I would recommend putting the data in another file separate for your class files, but if you insist on combining them, you can see:

  • JarInputStream or ZipInputStream to read the contents of a JAR file
  • Get JarEntry for the corresponding file
  • Read and modify the content as you wish.
  • JarOutputStream or ZipOutputStream to write content back

Make sure that you do not read the resource through the class path and that it comes from a file on disk / network.

+2
source

Source: https://habr.com/ru/post/1389322/


All Articles