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

5
  • you should use System.getProperty("user.dir"); to get the path of the current directory and concatenate the rest of the path you want Commented May 26, 2017 at 5:53
  • 2
    when you getResource("abc.access"), it looks it relatively to current class, so you probably need /abc.access, see also stackoverflow.com/questions/13967307/… Commented May 26, 2017 at 5: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 Commented May 26, 2017 at 6:04
  • @RC. This didn't work either Commented May 26, 2017 at 6:05
  • There are three variants of solution, depending on the situation: stackoverflow.com/a/56327069/715269 Commented May 27, 2019 at 13:37

5 Answers 5

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>
Sign up to request clarification or add additional context in comments.

Comments

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")

Comments

2

try this :

String filename = "abc.access";
InputStream in = getClass().getClassLoader().getResourceAsStream(filename);

1 Comment

or getResource(filename).
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.

Comments

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.