For several weeks I was wondering how to embedded one of my SQLIte database in my plugin. Sqlite database are simply binary files. Until now my plugin was loading the database using a hard coded path !!!! What a bad solution causing lot of nightmares for any developer !!!!
Today I had to export my plugin and as a consequence I had to find a solution.
Here the Eclipse magic operated once again. I took a look at FileLocator class.
Every Eclipse Plugin developer HAVE to know this class.
The find(bundle, path, override) method allowed me to get an URL for my database file. Then the toFileURL(url) allowed me to convert this URL into an other URL using the file protocol.
Here the magic operates: I was wondering how my JDBC driver would be able to load a database from a Jar file …. In fact this driver is not able to do that but Eclipse was clever enough according to the toFileUrl method Javadoc to:
“The contents of the URL may be extracted into a cache on the file-system in order to get a file URL”
Eclipse copies in that case my database file into the configuration directory of my Eclipse’s installation. This way my JDBC driver was able to load the database since it loaded it from a local file as it did for a hard coded file path !!!!