7

I have a maven module with the following directory structure and I want to get the path to abc.access in abcManager.java.

src
 |-main
    |-java
       |-org.abc.xyz
          |-init
             |-abcManager.java
    |-resources
       |-abc.access

I tried it using abcManager.class.getResource("abc.access") but it gives a null.

I went through the below questions but the solutions didn't work.

How to get the path of src/test/resources directory in JUnit?

Can't get path to resource

Asma Zinneera Jabir
  • 801
  • 3
  • 13
  • 31
  • you should use System.getProperty("user.dir"); to get the path of the current directory and concatenate the rest of the path you want – Fady Saad May 26 '17 at 05:53
  • 2
    when you `getResource("abc.access")`, it looks it relatively to current class, so you probably need `/abc.access`, see also https://stackoverflow.com/questions/13967307/reading-from-src-main-resources-gives-nullpointerexception?rq=1 –  May 26 '17 at 05:54
  • @user7790438 I tried it by copying the path of the file and removing the user dir part, gave the the path as System.getProperty("user.dir") + "rest-of-the-path". Still didn't work – Asma Zinneera Jabir May 26 '17 at 06:04
  • @RC. This didn't work either – Asma Zinneera Jabir May 26 '17 at 06:05
  • There are three variants of solution, depending on the situation: https://stackoverflow.com/a/56327069/715269 – Gangnus May 27 '19 at 13:37

5 Answers5

6

The problem was that I have not included the resource in the maven build in pom file. When I included the following it worked with abcManager.class.getResource("/abc.access")

<build>
    <resources>
      <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>abc.access</include>
                </includes>
            </resource>
    </resources>   
</build>
Asma Zinneera Jabir
  • 801
  • 3
  • 13
  • 31
3

According to the javadoc of URL java.lang.Class.getResource(String name) :

Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:

  • If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
    Otherwise,
  • the absolute name is of the following form:

    modified_package_name/name

Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').

When you invoke :

abcManager.class.getResource("abc.access")

You don't prefix the path with a "/" character . So, you use the second way.
It means that the resource should be located in the org.abc.xyz.init package (or folder) but it is not the case since the resource is located in the root of the resources folder.
In Maven, resources is the base directory for resources.
So you can get the resource by invoking the first way:

abcManager.class.getResource("/abc.access")

or you can also simply do it :

getClass().getResource("/abc.access")
davidxxx
  • 125,838
  • 23
  • 214
  • 215
2

try this :

String filename = "abc.access";
InputStream in = getClass().getClassLoader().getResourceAsStream(filename);
Sachin Sharma
  • 1,446
  • 4
  • 18
  • 37
0

Following should work.

InputStream resourceAsStream = 
                   abcManager.class.getClassLoader().getResourceAsStream("abc.access");

provided you are working inside an IDE (Ex. Eclipse). If you are trying this from commandline, you need to explicitely setup the classpath.

Shailesh Pratapwar
  • 4,054
  • 3
  • 35
  • 46
0

class.getResource is local to the class, in this case package org.abc.xyz.init. You are trying to read src/main/resources/org/ABC/xyz/init/abc.access. Alternatively to the class loader which always loads from the classpath root you can also do class.getResource("/abc.access")

Lev Kuznetsov
  • 3,520
  • 5
  • 20
  • 33