read map db file from an embedded resource in a fat jar

33 views
Skip to first unread message

Charles Monteiro

unread,
Apr 1, 2022, 11:19:44 AM4/1/22
to MapDB
I have the need to carry around a read only map db file as part of the resources for a fat jar i.e. I'm using groovy if it makes a diff, should not , I can read embedded plan text files from within the resource folder i.e. at runtime within a deployed fat jar but I cannot do the same for a map db file.

Java/Groovy are not my native tongue so maybe I'm missing something.

if I use the "getResourceAsStream" api , I am able to retrieve the file in question i.e. said file/resource can be found but the MapDB API expects a file

So first of all I"m wondering if its possible i.e. ever intended in the design, after all would not be able to write to the file in the first place, but in our case we just need read only .

We have been using in prod by deploying the map db file along with the jar , but the powers at be want to deliver the entire product as a single exe for which we plan to use launch4J i.e. if relevant to this issue

I appreciate the feedback, thanks

alla...@UToronto.CA

unread,
Apr 2, 2022, 4:42:26 PM4/2/22
to MapDB
Since a resource is not a file in and of itself you can't open it as a file.  It's just a bunch of bits inside a larger file.  There are some hacky ways I think can be used to do what you want but I have not tried them.

If you have write access to the file system, create a temporary file and stream the .db file to the temporary file, then open that.  When the program exits, the file will go away.  Make sure you flush the last beffer before opening the file for reading.

If your data is not huge, you could instead serialize the hashmap object and store that output file in the resource.  Then deserialize the object back into memory.  I suppose the whole reason to use MapDB is that the data is large so perhaps this will not work.

You could simply put a file of the data you need to access into the resource, read it into a memoryDirect instance and use it from there.  It would take time to load but would not take up any heap space.

None of the ideas are great but they could be made to work.
Reply all
Reply to author
Forward
0 new messages