23

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

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>groupId</groupId>
  <artifactId>artifactId</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <build>
   <sourceDirectory>src/main/java</sourceDirectory>
   <resources>
    <resource>
     <directory>src/main/resources</directory>
    </resource>
   </resources>
  </build>
</project>

In the src/main/resources directory I have a file called test. In the src/main/java directory I have a class that contains the following line:

System.out.println(this.getClass().getResourceAsStream("test"));

When the line of code is run within Eclipse, I get as output

java.io.BufferedInputStream@1cd2e5f 

When I export the project as .jar and run it I get as output

 null

Did I configure anything wrong?

hansi
  • 2,278
  • 6
  • 34
  • 42
  • 5
    Is the class a unit test or a real productive class? I assume a productive class. Than you have to change the getResourcesAsStream("test") into getResourcesAsStream("/test"); – khmarbaise Feb 05 '13 at 07:26
  • @khmarbaise you are right... but why is it working with eclipse ??? – ben75 Feb 05 '13 at 11:45
  • 1
    Is this test resource located in the *src/main/resources* folder or in *src/test/resources* folder with or without package name? Furthermore you don't need to configure the *src/main/java* nor the *src/main/resources* folder, cause they are default in maven. – khmarbaise Feb 06 '13 at 11:23

3 Answers3

16

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")

Rito
  • 3,092
  • 2
  • 27
  • 40
14

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>
Milind J
  • 517
  • 5
  • 10
2

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 :)

yelliver
  • 5,648
  • 5
  • 34
  • 65