![]() It's only visible to the same class loader as the one which loaded the class. However, the visibility of the properties file depends then on the class loader in question. ![]() InputStream input = classLoader.getResourceAsStream("/com/example/foo.properties") ClassLoader classLoader = getClass().getClassLoader() ![]() Only when you're using a "relative" class loader such as (), then you indeed need to start it with a /. Note that this path of a context class loader should not start with a /. InputStream input = classLoader.getResourceAsStream("com/example/foo.properties") If you have placed the foo.properties it in a Java package structure like com.example, then you need to load it as below ClassLoader classLoader = Thread.currentThread().getContextClassLoader() In for example Tomcat you can configure it as shared.loader property of Tomcat/conf/catalina.properties. You can alternatively also put it somewhere outside the default classpath and add its path to the classpath of the appserver. If you're using a Maven project, drop it in /main/resources folder. If you're developing a standard WAR project in an IDE, drop it in src folder (the project's source folder). If the propertiesfile is webapp-specific, best is to place it in /WEB-INF/classes. webapp's /WEB-INF/lib and /WEB-INF/classes, server's /lib, or JDK/JRE's /lib. Here foo.properties is supposed to be placed in one of the roots which are covered by the default classpath of a webapp, e.g. Properties properties = new Properties() InputStream input = classLoader.getResourceAsStream("foo.properties") So that you can load it by ClassLoader#getResourceAsStream() with a classpath-relative path: ClassLoader classLoader = Thread.currentThread().getContextClassLoader() There are basically three ways in a Java web application archive (WAR):
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |