0

I want to make my application config accessible outside of the application jar file.

So I want this structure.

Folder
-> myapp.jar
-> config.properties

So when I call my jar file via

java -jar myapp.jar I want it to use "config.properties".

But I have the following problem.

When I build myapp.jar the config.properties gets placed inside the myapp.jar. This means that if I want to change any of the properties in my conig.properties I cannot do this because its packaged inside the jar file.

Please could someone tell me how to get my config.properties packaged OUTSIDE my jar file.

Just to re-iterate

Current situation

Folder
-> myapp.jar (config.properties inside the jar)

Target situation

Folder
-> mypp.jar 
-> config.properties
TheDon2019
  • 19
  • 3
  • please could you link me to this question? I have not seen it before – TheDon2019 Mar 11 '19 at 13:33
  • @MariuszS Why have you marked the question as a duplicate? I have not asked how to read a file from the classpath. – TheDon2019 Mar 11 '19 at 13:42
  • I don't understand your question, sorry – MariuszS Mar 11 '19 at 13:59
  • @MariuszS I just edited the question, please check again – TheDon2019 Mar 11 '19 at 14:15
  • Just don't put the file into `.../src/main/resources`. It is technically not a resource file. – Henry Mar 11 '19 at 14:19
  • @Henry If I do that then the application doesn't run. a third party library makes the following call `InputStream fileStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);` – TheDon2019 Mar 11 '19 at 14:28
  • @Henry so it has to be in resources folder or something classed as resource. – TheDon2019 Mar 11 '19 at 14:29
  • In that case, it has to be a resource and you need to add its location to the classpath when you run the program. Still, don't put it into `.../src/main/resources` or it will end up in the jar. – Henry Mar 11 '19 at 14:32
  • @henry please could you suggest what I should do? If I create a folder and use intelliJ to ensure its defined as a resource how does that help? The resource and any other resource folders get put into the jar? – TheDon2019 Mar 11 '19 at 14:42
  • As far as IntelliJ is concerned don't say it is a resource. If you do that it gets packed into the jar. Then, when you run the program use something like `java -cp myapp.jar:config-folder MyMainClass` (if you are on Linux - the Windows path syntax uses `;` instead of `:`). If there are other dependencies, add them to the classpath as well. – Henry Mar 11 '19 at 15:01
  • @Henry as stated above I have to define it as a resource under a resources folder or I have to create another folder and define it as a resource. This is because a third party library calls `getResourceAsStream(filename)' I don't have control of that part of code. – TheDon2019 Mar 11 '19 at 15:15
  • I will use maven, then this will be trivial... I never build project using IntelliJ directly. And I see you have tagged this question using maven, so why are you using Intellij to build this jar? – MariuszS Mar 11 '19 at 16:59

0 Answers0