Re: Housing and reading internal resources using Tomcat 8.5.4 and JDK 1.8
Hi Chris, Thank you for the responses, yes those both help. I did a lot of reading yesterday and was unable to get a clear answer at how this should be done with JEE, where internal resource files are expected to reside (besides just generally being under WEB-INF) within the deployment, and the String value that should be given to ClassLoader.getResource(value) to be able to look them up. Hence why I reached out to the mailing list. I have in fact created a properties directory under WEB-INF/classes, for organizing purposes, and ClassLoader.getResource("properties/xyz.txt") does appear to be working. Thank you, Doug On Tue, Aug 30, 2016 at 11:27 AM, Christopher Schultz < ch...@christopherschultz.net> wrote: > -BEGIN PGP SIGNED MESSAGE- > Hash: SHA256 > > Doug, > > On 8/29/16 6:29 PM, Doug Gschwind wrote: > > 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? > > - From a servlet, you'll want to to: > >URL url = > getServletContext().getResource("/WEB-INF/properties/xyz.txt"); > > If you don't have access to any of the servlet API objects, you might > have to move your properties file into the WEB-INF/classes/ directory, > or into a JAR file in the WEB-INF/lib/ directory. Then you can do this: > > URL url = > Thread.currentThread().getContextClassLoader().getResource("/xyz.txt"); > > Then do whatever you want with that URL. For example: > > Properties props = new Properties(); > try(InputStream in = url.openStream()) { > props.load(in); > } > > // Now use your properties > > Hope that helps, > - -chris > -BEGIN PGP SIGNATURE- > Comment: GPGTools - http://gpgtools.org > Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ > > iQIcBAEBCAAGBQJXxdAoAAoJEBzwKT+lPKRYCY8QALl8QQcldOeMXYlgDarYtGOH > lZQbxlhn+nmzMOHROESGZPefqZUoEQaplWSSivUayYDjvNMGE9SkkO4tbZpy/+Ut > xZhpj4gjnC6E3gRN8S8SZf3gchNQLIOqtxDjXQBNZakm8jAeNhXDxK0CkiSDfumm > 8XqaUS1TJulEftoWO8WeGJ1/2dOKOdllCM7HoF0H1jbY1YYnpTbPnl8CBAkhHGa6 > 2BSKgP68kuSxeAnzX5fmE9NsNXbDdPnhWErUZ5xmER+Eol/oxTqaUyT+1q7m7L2R > XSv+gfvSdp/2Y7pZA44zhlZo1AjgxXBSR28NdldZtMoOR/tUws74fx3XGP7AXud8 > zIrHYxFCpSv4rE4npO3Bcwfi2IXpx9t208Dh9k0U0muF/KAjKeZ7EV2pHc4J3ZpE > Lw89g9RAKPZlg6WCfARrvRGDXELjoPfmz2l3/AFxAiAygjkdDDECdadEdiqtvX9i > 7YnnT6uXUW9VIySSSuuA4xFdoJFHbsW2RMrgIEXuF6QnTfha88/sq8loYf5lmsRw > Cg+zSvU3p2WnMn37y60ABosHoXZdlze5Aq7vh4EMHEmVVLbPvK3J9EPtCCmwHJtq > a70JGkCZf22pMLFj4rLXu6ZNo39pTdd6RCePUdtlFo4/YyL75VF8g8yXgO2OSiRz > 6IUM9PXjuI4QipZ4xCyH > =WqVq > -END PGP SIGNATURE- > > - > To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org > For additional commands, e-mail: users-h...@tomcat.apache.org > >
Re: Housing and reading internal resources using Tomcat 8.5.4 and JDK 1.8
-BEGIN PGP SIGNED MESSAGE- Hash: SHA256 Doug, On 8/29/16 6:29 PM, Doug Gschwind wrote: > 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? - From a servlet, you'll want to to: URL url = getServletContext().getResource("/WEB-INF/properties/xyz.txt"); If you don't have access to any of the servlet API objects, you might have to move your properties file into the WEB-INF/classes/ directory, or into a JAR file in the WEB-INF/lib/ directory. Then you can do this: URL url = Thread.currentThread().getContextClassLoader().getResource("/xyz.txt"); Then do whatever you want with that URL. For example: Properties props = new Properties(); try(InputStream in = url.openStream()) { props.load(in); } // Now use your properties Hope that helps, - -chris -BEGIN PGP SIGNATURE- Comment: GPGTools - http://gpgtools.org Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQIcBAEBCAAGBQJXxdAoAAoJEBzwKT+lPKRYCY8QALl8QQcldOeMXYlgDarYtGOH lZQbxlhn+nmzMOHROESGZPefqZUoEQaplWSSivUayYDjvNMGE9SkkO4tbZpy/+Ut xZhpj4gjnC6E3gRN8S8SZf3gchNQLIOqtxDjXQBNZakm8jAeNhXDxK0CkiSDfumm 8XqaUS1TJulEftoWO8WeGJ1/2dOKOdllCM7HoF0H1jbY1YYnpTbPnl8CBAkhHGa6 2BSKgP68kuSxeAnzX5fmE9NsNXbDdPnhWErUZ5xmER+Eol/oxTqaUyT+1q7m7L2R XSv+gfvSdp/2Y7pZA44zhlZo1AjgxXBSR28NdldZtMoOR/tUws74fx3XGP7AXud8 zIrHYxFCpSv4rE4npO3Bcwfi2IXpx9t208Dh9k0U0muF/KAjKeZ7EV2pHc4J3ZpE Lw89g9RAKPZlg6WCfARrvRGDXELjoPfmz2l3/AFxAiAygjkdDDECdadEdiqtvX9i 7YnnT6uXUW9VIySSSuuA4xFdoJFHbsW2RMrgIEXuF6QnTfha88/sq8loYf5lmsRw Cg+zSvU3p2WnMn37y60ABosHoXZdlze5Aq7vh4EMHEmVVLbPvK3J9EPtCCmwHJtq a70JGkCZf22pMLFj4rLXu6ZNo39pTdd6RCePUdtlFo4/YyL75VF8g8yXgO2OSiRz 6IUM9PXjuI4QipZ4xCyH =WqVq -END PGP SIGNATURE- - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: Housing and reading internal resources using Tomcat 8.5.4 and JDK 1.8
> 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
Housing and reading internal resources using Tomcat 8.5.4 and JDK 1.8
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