I have a project structure like this:
src
|-main
|-java
|-com.abc.xyz
|-Login.java
I have to add a resource file to t
from menu Run/edit configuration in VM option you should add -Dspring.config.location=path-file
I've tried it in IntelliJ, and it works!
As @maba pointed out, your properties file should be in the same package as your class for your code to work.
So, you should have two files:
If IntelliJ is showing the resource or not is beside the question. What you need to do is check if the results are included in your target artefact.
Do a build all
in IntelliJ, open up the resulting WAR/JAR/EAR with your favorite ZIP viewer and browse into the "com/abc/xyz" folder. You should see both files there.
Only solution worked for me:
File -> Project Structure -> Modules -> Dependencies Tab -> + Sign -> JARs or directories -> select resources directory -> Classes
I had the same problem and noticed that the resource file, for example: my.properties
is not copied to the corresponding module folder in the target
directory after build occurres. In order to solve that, I had to instruct Maven to copy the resources from the module directory to the target
directory during the build process. In the .pom
file I added <resource>
element like that:
<project ...>
...
<build>
...
<resource>
<directory>src/main/java/com/abc/xyz</directory>
<targetPath>com/abc/xyz</targetPath>
</resource>
</build>
...
</project>
Note that the <directory>
element is relative to the location of the .pom
file , i.e. the root directory of the project, and the <targetPath>
element indicates the package name separated by slashes.
Follow these two steps
1) Create a directory
Right Click ==> New ==> Directory
2) Mark Directory as Resources Root
Right Click on the Direcory ==> Mark Directory as ==> Resources Root
The launcher.properties
should not be under a folder called Login
. It should be placed directly in the src/main/resources/com/abc/xyz
folder.
It is really as simple as I said but if the resources folder is not marked as a sources folder then this may be the problem.
This is the initial class and setup:
Now create the resources folder:
This newly created folder should be automatically marked as a sources folder and if it is blue color marked then it is. Otherwise you'll have to mark it manually:
Now you'll be able to add packages to it:
And now you can add the file to it:
And rerunning the application will not give you any null
value back:
And the package view will surely show the launchers.properties
file as well: