tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christoph Nenning <Christoph.Nenn...@lex-com.net>
Subject Re: Housing and reading internal resources using Tomcat 8.5.4 and JDK 1.8
Date Tue, 30 Aug 2016 07:02:50 GMT
> Hello everyone,
> 
> I have a file xyz.txt that is specific to my web application which needs 
to
> be located by my web application, and I wish to find that resource via
> getClass().ClassLoader().getResource("xyz.txt") at runtime. The xyz.txt
> file has no relation to any particular Java class in our application. 
This
> resource is used internally by the application and should not be served
> directly by the container to inbound HTTP requests, therefore I have it
> located in the WEB-INF/properties directory of my web app deployment.
> However, the getResource("xyz.txt") method returns null, even though my
> xyz.txt file is certainly where it is expected to be found.
> 
> Where should I place this file ideally, and given that file exists in 
that
> location, what is the parameter value I need to pass to getResource() so 
it
> returns a non-null value?
> 
> Thanks, Doug


Hi,

the file must be present in classpath. For a webapp that means 
WEB-INF/classes. When you place it not in a sub dir you must pass 
"/xyz.txt" to getResource() (note the leading slash).

If you use eclipse for development you can create a "source folder" and 
place it there. eclipse will take care of copying it to WEB-INF/classes. 
Other IDEs or build tools call that a resources dir. In maven it defaults 
to src/main/resources.


Regards,
Christoph

This Email was scanned by Sophos Anti Virus

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message