Java Resource Files: Classpath Problem

319 views
Skip to first unread message

Michael Hermus

unread,
Dec 16, 2012, 9:57:12 PM12/16/12
to google-a...@googlegroups.com
Hi,

I have a .txt file which I need to load as Resource using getResourceAsStream(). The docs (https://developers.google.com/appengine/kb/java) say:

It is possible to read from a file which is uploaded as part of your application provided that it is in the following locations:

  • war/WEB-INF
  • in a location matching the <resource-files> pattern in appengine-web.xml (which by default includes everything)

I have no <resource-files> entry (so it should use the default), and I put this file in both the war/WEB-INF directory AND the war root directory: AppEngine CANNOT load the resource. However, if I put the file in war/WEB-INF/classes, the system CAN load the resource. I spent way too much time trying to get this to work; perhaps I misunderstand something, but if not, please update the docs, or make this work as documented.

Mike

Joakim

unread,
Dec 17, 2012, 9:47:25 AM12/17/12
to google-a...@googlegroups.com
As the App Engine documentation notes, you can use many of the file access facilities of the Java API.
The methods java.lang.Class.getResource(?:AsStream)? and java.lang.ClassLoader.getResource(?:AsStream)? are special cases. While the documentation for Class.getResourceAsStream is quite somewhat convoluted, it is only logical that it searches relative to the .class file you used:
> InputStream stream = MyClass.class.getResourceAsStream("myFile");
This is true of all Java environments.
To access files outside the classpath, I suggest using the java.io family of classes.
Reply all
Reply to author
Forward
0 new messages