23

I have a problem for a couple of hours, and I tried all the solutions I've found on tutorials. It's simple: I can't access the resource files. I try to open a file I've put in src/main/resources and src/test/resources.

I have a simple Java project and I use Maven, with Eclipse as IDE, with the m2e plugin. I want to use resources filtering with Maven, with different profiles, and there's my POM.xml:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>2.3.2</version>
      <configuration>
    <source>1.5</source>
    <target>1.5</target>
    <debug>false</debug>
    <optimize>true</optimize>
      </configuration>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-resources-plugin</artifactId>
      <version>2.4</version>
      <configuration>
    <encoding>UTF-8</encoding>
      </configuration>
    </plugin>
  </plugins>

  <filters>
    <filter>src/main/filters/${env}.properties</filter>
  </filters>

  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
    </resource>
    <resource>
      <directory>src/test/resources</directory>
      <filtering>true</filtering>
    </resource>
  </resources>
</build>

<profiles>
  <profile>
    <id>LOCAL</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
      <env>local</env>
    </properties>
      </profile>
</profiles>

And I made a simple test, in src/java/test :

@Test
public void testOpenResourceFile() {
    File testf=new File("/test.txt");
    assertTrue(testf.exists());
}

So, in Eclipse, I run (on my project folder, in the package view) :

  • Run as > Maven build > process-resources
  • Run as > Maven build > process-test-ressources
  • Run as > Maven build > compile

With env: LOCAL

In the test, I do: Run as > Junit Test Case. But it fail... I looked in target/test-classes directory generated by Maven, and the file test.txt is there.

Did I missed some steps during my project's compilation, or is there a problem with my configuration?

EDIT:
I tried with File("test.txt") and File("../test.txt") as well.

Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
Iraes
  • 233
  • 1
  • 2
  • 5
  • 2
    Possible duplicate of http://stackoverflow.com/questions/1464291/how-to-really-read-text-file-from-classpath-in-java – samlewis Nov 11 '13 at 22:06
  • Welcome to SO! What does "fail" mean in this case? You get an error? There's no files present in the directory? Etc.? – Derek Nov 11 '13 at 22:20
  • testf.exists() = false in this case, so the assert makes the test to fail. If I try to read the file, I get an IOException. – Iraes Nov 11 '13 at 22:23

4 Answers4

43

It looks to me like your problem is

File testf = new File( "/test.txt" );

That's looking for a file called test.txt at the root of your computer's filesystem. What you want is the root of the resource tree, which you get with the getResource method:

File testf = new File( this.getClass().getResource( "/test.txt" ).toURI() );

Or, in static context, use the name of the containing class:

File testf = new File( MyClass.class.getResource( "/test.txt" ).toURI() );

Of course you'll need to add error handling to that example.

Sam Hanes
  • 2,789
  • 23
  • 22
  • 1
    @AHK I added an example of how to do it from static context. – Sam Hanes Mar 28 '16 at 21:03
  • @Sam Hanes For me the file location is clearly shown when I get it printed through a system out print. But when I execute the following command File file = new File(this.getClass().getResource()("filename").getFile(). exists () it returns false. Any idea why? I have been struggling with this for last 2 days. – raikumardipak Feb 17 '18 at 09:27
6

Check your <build> tag in pom.xml it should be something like this, faced the same issue and adding this <resources> tag in the <build> worked for me.

<build>
    <resources>
        <resource>
            <filtering>true</filtering>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
</build>
Vandit Upadhyay
  • 315
  • 3
  • 8
5

I had the same problem, just make sure that the "text.txt" file is in the resources folder of the test project (src/test/resources/text.txt) and not in any other resources folder. Also, you should retrieve the file from the resources folder like this:

this.getClass().getClassLoader().getResource("text.txt").getFile();

If this still does not work then try to set the useSystemClassLoader from the maven-surefire-plugin to false in your pom.xml.

http://maven.apache.org/surefire/maven-surefire-plugin/examples/class-loading.html

Vivek Chavda
  • 473
  • 1
  • 4
  • 16
Marco
  • 2,445
  • 2
  • 25
  • 15
  • Using `URL.getFile()` will fail silently for non-file resources. It would be better to use `URL.toURI()` with the `File` constructor that takes a `URI`, as that will raise an exception if the resource is not a local file path. – Sam Hanes Mar 28 '16 at 21:09
0

I made this method for this job:

public static String getPropaccess(String key){
    String value="";
    Properties configFile = new Properties();
    try {
        configFile.load(Propaccess.class.getClassLoader().getResourceAsStream("test/resources/config.properties"));
        value = configFile.getProperty(key);
        return value;
    } catch (IOException e) { 
        e.printStackTrace();
    }
    return value;
}
Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58