Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding resources in IntelliJ for Maven project

I have a project structure like this:

src  |-main     |-java        |-com.abc.xyz           |-Login.java 

I have to add a resource file to this and read the resource with

InputStream is = getClass().getResourceAsStream("launchers.properties"); 

This is giving null.

In Intellij I am not able to add a new package under src/main for resources folder so that the project structure looks like this. How can I load the launchers.properties resource file into the project?

src  |-main     |-java        |-com.abc.xyz           |-Login.java     |-resources        |-com.abc.xyz           |-Login              |-launcher.properties 

I tried the solution suggested by @maba but still not working

like image 326
vikas Avatar asked Sep 10 '13 10:09

vikas


People also ask

How do I add resources to IntelliJ?

From the main menu, select File | Project Structure Ctrl+Alt+Shift+S and click Project Settings | Modules. Select the necessary module and then open the Sources tab in the right-hand part of the dialog. Click Add Content Root and specify the folder that you want to add as a new content root.

How do I add Maven to an existing IntelliJ project?

Add Maven support Open an existing project, for example, a Java project. In the Project tool window, right-click your project and select Add Framework Support. In the dialog that opens, select Maven from the options on the left and click OK.


2 Answers

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:

enter image description here

Now create the resources folder:

enter image description here

enter image description here

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:

enter image description here

Now you'll be able to add packages to it:

enter image description here

enter image description here

And now you can add the file to it:

enter image description here

enter image description here

And rerunning the application will not give you any null value back:

enter image description here

And the package view will surely show the launchers.properties file as well:

enter image description here

like image 68
maba Avatar answered Sep 21 '22 07:09

maba


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:

  • src/main/java/com/abc/xyz/Login.java
  • src/main/resources/com/abc/xyz/launcher.properties

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.

  • If they are, you are doing something wrong in your code. Check for typos, especially dots and spaces at the end or beginning (e.g. "launcher.properties[space]"), copy/paste the file name to make sure
  • If they are not there, your IntelliJ setup is wrong. Resources do not get included in your target build. Check online for tutorials how to do this with IntelliJ idea.
like image 35
boky Avatar answered Sep 23 '22 07:09

boky