简体   繁体   English

在src / main / resources中的文件的Java路径

[英]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.accessabcManager.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目录的路径?

Can't get path to resource 无法获得资源的路径

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_pa​​ckage_name /名称

Where the modified_package_name is the package name of this object with '/' substituted for '.' 其中modified_pa​​ckage_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中, resourcesresources的基本目录。
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.

相关问题 java.io.FileNotFoundException:即使文件存在于 src/main/resources 中,类路径资源 - java.io.FileNotFoundException: class path resource even though file exists in src/main/resources 如何从Java获取src / main / resources路径 - How to get src/main/resources path from Java 如何获取 src/main/resources 文件夹中文件的路径? - How to get the path to a file in src/main/resources folder? 如何获取驻留在src / main / resources中的.exe文件的路径 - How to get the path for .exe file which resides in src/main/resources src / main / resources中的文件不在classpath中 - file in src/main/resources is not in classpath Java 无法访问 src/main/resources 中的 txt 文件 - Java can't access txt file in src/main/resources java.io.FileNotFoundException: src\\main\\resources\\Report\\testQuery.xlsx (系统找不到指定的路径) 在tomcat中部署文件后 - java.io.FileNotFoundException: src\main\resources\Report\testQuery.xlsx (The system cannot find the path specified) after deploying file in tomcat src / main / resources未添加在战争类路径中 - src/main/resources not added in war class path mkdir -p src / main / java src / main / resources语法错误? - mkdir -p src/main/java src/main/resources Syntax Error? 如何在不给用户名路径加上目录路径(“ /A/B/src/main/resources/x.xml”)的情况下读取Java类中的xml文件? - How to read an xml file in Java class without giving a user name path an just with directory path(“ /A/B/src/main/resources/x.xml”)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM