[英]Java path to file in src/main/resources
I have a maven module with the following directory structure and I want to get the path to abc.access
in abcManager.java.
我有一个具有以下目录结构的maven模块,我想在
abc.access
中abcManager.java.
的路径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. 我尝试使用
abcManager.class.getResource("abc.access")
但它给出了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? 如何在JUnit中获取src / test / resources目录的路径?
The problem was that I have not included the resource in the maven build in pom file. 问题是我没有将资源包含在pom文件中的maven构建中。 When I included the following it worked with
abcManager.class.getResource("/abc.access")
当我包含以下内容时,它使用了
abcManager.class.getResource("/abc.access")
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>abc.access</include>
</includes>
</resource>
</resources>
</build>
try this : 试试这个 :
String filename = "abc.access";
InputStream in = getClass().getClassLoader().getResourceAsStream(filename);
According to the javadoc of URL java.lang.Class.getResource(String name)
: 根据
URL java.lang.Class.getResource(String name)
的javadoc:
Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:
在委派之前,使用此算法从给定资源名称构造绝对资源名称:
- If the name begins with a '/' ('\/'), then the absolute name of the resource is the portion of the name following the '/'.
如果名称以'/'('\\ u002f')开头,则资源的绝对名称是'/'后面的名称部分。
Otherwise,除此以外,
the absolute name is of the following form:
绝对名称具有以下形式:
modified_package_name/name
modified_package_name /名称
Where the modified_package_name is the package name of this object with '/' substituted for '.'
其中modified_package_name是此对象的包名称,其中'/'替换为'。' ('\.').
( '\\ 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. 这意味着资源应位于
org.abc.xyz.init
包(或文件夹)中,但事实并非如此,因为资源位于resources
文件夹的根目录中。
In Maven, resources
is the base directory for resources. 在Maven中,
resources
是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")
Following should work. 以下应该工作。
InputStream resourceAsStream =
abcManager.class.getClassLoader().getResourceAsStream("abc.access");
provided you are working inside an IDE (Ex. Eclipse). 如果您在IDE(Ex.Eclipse)中工作。 If you are trying this from commandline, you need to explicitely setup the classpath.
如果从命令行尝试此操作,则需要明确设置类路径。
class.getResource is local to the class, in this case package org.abc.xyz.init. class.getResource是类的本地,在本例中是包org.abc.xyz.init。 You are trying to read src/main/resources/org/ABC/xyz/init/abc.access.
您正在尝试读取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")
除了始终从类路径根加载的类加载器之外,您还可以执行class.getResource(“/ abc.access”)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.