roemer
roemer

Reputation: 537

GAE resource file FileNotFoundException

In my appconfig.xml I have:

<resource-files>
    <include path="/emailtemplates/**.html" />
</resource-files>

When I try to read a file I always get a FileNotFoundException. I've tried it with and without a trailing / but it doesn't make any difference. I've also checked that the files exist in the packaged app.

My project consists of modules and I'm trying this on my local dev system on windows. Am I doing something wrong here?

Upvotes: 0

Views: 374

Answers (2)

roemer
roemer

Reputation: 537

First of all, I moved my resource folder into the WEB-INF folder. The advantage is simply that I don't need any extra configuration in my appengine-web.xml which eliminates one point of failure.

Second, I now use the ServletContext to get the full path to my resource file. Before I just used path as argument for new File and that didn't work. That didn't come out anywhere in the docs I think. So my working piece of code to read a text file looks like:

StringBuilder sb = new StringBuilder();
Scanner scanner = new Scanner(new File(context.getRealPath(path)), "UTF-8");
while (scanner.hasNextLine())
    sb.append(scanner.nextLine());
scanner.close();

Upvotes: 0

konqi
konqi

Reputation: 5217

it's difficult to say what's wrong when you give so little information. However I can give you an example of how i use resources.

  • I use maven so my resource files go to src\main\resources\*
  • In code I use this.getClass().getResource("/" + filename).getPath() to get the full path (note that in this case the '/' refers to the root of where the resources are. Usually '/' would refer to the root directory in the file system or in the working directory in Java)
  • Then open the file like you'd normally do, ie. new FileInputStream(pathFromAbove)

My pom.xml is configured to include my resources:

<build>
    <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>
        ...

Upvotes: 1

Related Questions