Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java path to file in src/main/resources

Tags:

java

maven

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

like image 209
Asma Zinneera Jabir Avatar asked May 26 '17 05:05

Asma Zinneera Jabir


People also ask

How do you create a file in src main resources in Java?

Java creating file with FilesPath path = Paths. get("src/main/resources/myfile. txt"); A Path object is created.

What goes in src main resources?

src/main/resources – configuration files and others such as i18n files, per-environment configuration files, and XML configurations. src/main/webapp – for web applications, contains resources like JavaScript, CSS, HTML files, view templates, and images.


3 Answers

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>
like image 126
Asma Zinneera Jabir Avatar answered Oct 08 '22 20:10

Asma Zinneera Jabir


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")
like image 28
davidxxx Avatar answered Oct 08 '22 18:10

davidxxx


try this :

String filename = "abc.access";
InputStream in = getClass().getClassLoader().getResourceAsStream(filename);
like image 2
Sachin Sharma Avatar answered Oct 08 '22 19:10

Sachin Sharma