getClass().getResourceAsStream() in maven project

前端 未结 3 738
轻奢々
轻奢々 2021-02-18 20:58

The pom.xml of my maven project looks as follows:



        
相关标签:
3条回答
  • Under <build><resources> make sure to have the <include> as below This will explicitly tell maven to fetch these files and include it in the build.

       <build>
         <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
         </resources>
         ...
      </build>
    
    0 讨论(0)
  • 2021-02-18 21:57

    I got this error but through ton of Googling, I could not find the solution. All the answers are about the path, nobody cares that It can run in Eclipse but not in exported jar file :(

    But now I found solution, so simple: In Eclipse right click on your maven project-> Properties -> Java build path -> Source Tab

    You can see a tree like :

    MyProject/src
       Ouput follder..
       Included (**/*.java)
       Excluded
       ...
    

    Double click on Included (**/*.java), remove the existing one so It become Included (All) Now export the jar file :)

    0 讨论(0)
  • 2021-02-18 22:01

    I was facing this same problem.

    When you are trying to get the resource like this:
    getClass().getResourceAsStream("test")

    You are trying to find the resource relative to that package.

    To get the resource from the src/main/resources directory, you have to put a slash before the resource name.
    getClass().getResourceAsStream("/test")

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