getResourceAsStream returns null

前端 未结 16 2302
臣服心动
臣服心动 2020-11-22 04:00

I\'m loading a text file from within a package in a compiled JAR of my Java project. The relevant directory structure is as follows:

/src/initialization/Life         


        
相关标签:
16条回答
  • 2020-11-22 04:41

    The rules are as follows:

    1. check the location of the file you want to load inside the JAR (and thus also make sure it actually added to the JAR)
    2. use either an absolute path: path starts at the root of the JAR
    3. use an relative path: path starts at the package directory of the class you're calling getResource/ getResoucreAsStream

    And try:

    Lifepaths.class.getResourceAsStream("/initialization/Lifepaths.txt")
    

    instead of

    Lifepaths.class.getClass().getResourceAsStream("/initialization/Lifepaths.txt")
    

    (not sure if it makes a difference, but the former will use the correct ClassLoader/ JAR, while I'm not sure with the latter)

    0 讨论(0)
  • 2020-11-22 04:43

    There seems to be issue with the ClassLoader that you are using. Use the contextClassLoader to load class. This is irrespective of whether it is in a static/non-static method

    Thread.currentThread().getContextClassLoader().getResourceAsStream......

    0 讨论(0)
  • 2020-11-22 04:43

    Roughly speaking:

    getClass().getResource("/") ~= Thread.currentThread().getContextClassLoader().getResource(".")

    Suppose your project structure is like the following:

    ├── src
    │   ├── main
    │   └── test
    │   └── test
    │       ├── java
    │       │   └── com
    │       │       └── github
    │       │           └── xyz
    │       │               └── proj
    │       │                   ├── MainTest.java
    │       │                   └── TestBase.java
    │       └── resources
    │           └── abcd.txt
    └── target
        └── test-classes
            ├── com
            └── abcd.txt
    
    
    // in MainClass.java
    this.getClass.getResource("/") -> "~/proj_dir/target/test-classes/"
    this.getClass.getResource(".") -> "~/proj_dir/target/test-classes/com/github/xyz/proj/"
    Thread.currentThread().getContextClassLoader().getResources(".") -> "~/proj_dir/target/test-classes/"
    Thread.currentThread().getContextClassLoader().getResources("/") ->  null
    
    
    0 讨论(0)
  • 2020-11-22 04:44

    I found myself in a similar issue. Since I am using maven I needed to update my pom.xml to include something like this:

       ...
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>/src/main/resources</directory>
            </resource>
            <resource>
                <directory>../src/main/resources</directory>
            </resource>
        </resources>
        <pluginManagement>
            ...
    

    Note the resource tag in there to specify where that folder is. If you have nested projects (like I do) then you might want to get resources from other areas instead of just in the module you are working in. This helps reduce keeping the same file in each repo if you are using similar config data

    0 讨论(0)
提交回复
热议问题