Search code examples
javajdbclaunch4j

Best way to distribute a Java app with resource files (DB connection parameters, images, ...)


I want to distribute a Java application that connects to an external database using a Java properties file. My issue is that once my project is exported as a Jar file (and then as an EXE using Launch4j) in a different place than my project's root folder, the connection to my database fails and most of my pictures are not loaded.

Regarding just the connection, I have tried several things to read the connection properties written in an external file, which is located in a source folder :

Project structure

The code :

Code reading the properties file with the connection parameters

I also tried to let the property file in the root folder of my project: both way work when running the project, but fail when launching the jar or the exe.

First I thought it was just because of the exportation of my project, that maybe failed due to some wrong settings. But I am started to think that I may have a wrong understanding on how to import resources into a Java project, even thought I followed several topics on that subject on Stackoverflow.

So :

  • What is the best way to include properties files in a way that makes it possible to export them in a Jar/exe file ?
  • Or, should these files be included after generating the Jar, by referencing them when producing the exe ? How would that be ?

As the connection file contains some sensible data (ie. the password to connect to the database), I hoped there was an official way to keep that file protected from unwanted access after exporting the Jar/exe file.

Thank you a lot for helping. I can provide more code and pictures if needed.


Solution

  • Resources (as in a jar) are not File (on the file system). Use

    getClass().getResourceAsStream("/conf/connexxion.properties");`
    

    For that place the conf under /resources.