46

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

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
vikas
  • 1,318
  • 4
  • 16
  • 33

7 Answers7

75

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

maba
  • 47,113
  • 10
  • 108
  • 118
  • Thanks @maba, it is not working. It is still giving null, and one more thing, when I change the intellij's view into `Packages` I am not seeing the properties file under xyz. I am assuming that the properties file is not available for that class. The problem in my opinion is that being not able to create a package under `resources` directory. – vikas Sep 10 '13 at 11:06
  • thank you for such a beautiful illustration..I really appriciate it.....I tried what you suggested, but no luck :( I have attached my intellij snapshot http://imageshack.us/photo/my-images/96/v6cn.png/ – vikas Sep 10 '13 at 15:38
  • No..... the structure is wrong.... you should not create the same package under resources, that is ugly and not proper: `resources` is for resources, and should not contain source packages. – WesternGun Sep 26 '18 at 14:26
  • Creating a package under the resources folder did not work for me. Rather just creating a properties file under the resources folder should suffice ! – arpiagar Sep 04 '19 at 13:43
4

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.
boky
  • 815
  • 5
  • 10
  • The `launchers.properties` is in the class path with the structure pointed by @maba. But still in the code null is returned. Does it has something to do with UiAutomator, as I am creating the java project to run UiAutomator test cases. – vikas Sep 10 '13 at 11:41
  • The properties file is added to project `classes` directory. But I am not finding com/abc/xyz in jar. Let me check why. – vikas Sep 10 '13 at 11:51
3

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

dasunse
  • 2,839
  • 1
  • 14
  • 32
0

No..... the structure is wrong.... you should not create the same package under resources, that is ugly and not proper: resources is for resources, and should not contain source packages.

When using ClassLoader.getResources(asStream)(path) method, the method just starts searching from the root of the classpath and the path name cannot start with / as leading characters. What you have to do, is to mark the resources as resources folder in IntelliJ. Then the files under resources will be listed under classpath and you can easily use them like you have done.

(I see in previous answers this option is not available yet in 2013, you only have mark as source folder, just like in Eclipse till now we have "add source folder", but now in 2018 it is available in Intellij: you can mark a folder as source, resources, test source, test resources, and all of them will be add to the root of classpath. )

WesternGun
  • 11,303
  • 6
  • 88
  • 157
0

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.

vivanov
  • 1,422
  • 3
  • 21
  • 29
0

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!

0

Only solution worked for me:

File -> Project Structure -> Modules -> Dependencies Tab -> + Sign -> JARs or directories -> select resources directory -> Classes

acanbiler
  • 153
  • 1
  • 8