{"id":23,"date":"2012-08-06T11:20:41","date_gmt":"2012-08-06T11:20:41","guid":{"rendered":"http:\/\/softwareeveryday.wordpress.com\/?p=23"},"modified":"2012-11-17T12:05:23","modified_gmt":"2012-11-17T12:05:23","slug":"loading-properties-files-from-classpath","status":"publish","type":"post","link":"https:\/\/www.softwareeverydayblog.com\/?p=23","title":{"rendered":"Loading properties files from classpath"},"content":{"rendered":"<p>You could load resources using\u00a0ClassLoader.getResourceAsStream() or\u00a0Class.getResourceAsStream(). Important thing to note is that\u00a0Class.getResourceAsStream (String)\u00a0delegates the call to\u00a0ClassLoader.getResourceAsStream(String). You can see the same below.<\/p>\n<pre lang=\"java\" line=\"1\">\r\npublic InputStream getResourceAsStream(String name) {\r\n\tname = resolveName(name);\r\n\tClassLoader cl = getClassLoader0();\r\n\tif (cl==null) {\r\n\t\t\/\/ A system class.\r\n\t\treturn ClassLoader.getSystemResourceAsStream(name);\r\n\t}\r\n\treturn cl.getResourceAsStream(name);\r\n}\r\n<\/pre>\n<p><strong>resolveName(name)<\/strong> will add a package name prefix if the name is not absolute. Remove leading &#8220;\/&#8221;\u00a0\u00a0if name is absolute. What does this mean? This means that if we pass just &#8220;test.properties&#8221; to\u00a0getResourceAsStream it will resolve it to &#8220;com\/my\/test.properties&#8221; (assuming your current package is com.my). If we pass &#8220;\/test.properties&#8221; it will assume you&#8217;re passing an absolute path and remove &#8220;\/&#8221; making it just\u00a0&#8220;test.properties&#8221;. Remember your classpath must have &#8220;com\/my\/test.properties&#8221; or\u00a0&#8220;test.properties&#8221; depending on what you pass to\u00a0getResourceAsStream().\u00a0Now, if you are using\u00a0ClassLoader.getSystemResourceAsStream() directly we know from the above code that it expects absolute paths without the &#8220;\/&#8221; in the beginning.<\/p>\n<p>Here is a sample folder\u00a0structure\u00a0and App.java the reads properties files from the classpath. Remember that the src\/main\/resources folder must be added (and usually is added) to the classpath.<\/p>\n<p><a href=\"https:\/\/www.softwareeverydayblog.com\/wp-content\/uploads\/2012\/08\/paths31.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-28\" title=\"paths3\" src=\"https:\/\/www.softwareeverydayblog.com\/wp-content\/uploads\/2012\/08\/paths31.png\" alt=\"\" width=\"210\" height=\"258\" \/><\/a><\/p>\n<pre lang=\"java\" line=\"1\">\r\npackage com.my;\r\n\r\nimport java.io.InputStream;\r\nimport java.util.Properties;\r\n\r\npublic class App\r\n{\r\n\tpublic static void main( String[] args )\r\n\t{\r\n\r\n\t\tString var = null;\r\n\t\tProperties properties = new Properties();\r\n\t\t\r\n\t\ttry {\r\n\r\n\t\t\tInputStream in = App.class.getResourceAsStream(\"test.properties\");\r\n\t\t\tproperties.load(in);\r\n\t\t\tvar = properties.getProperty(\"my.prop\");\r\n\t\t\tSystem.out.println(var);\r\n\r\n\t\t\tin = App.class.getResourceAsStream(\"\/test2.properties\");\r\n\t\t\tproperties.load(in);\r\n\t\t\tvar = properties.getProperty(\"my.prop\");\r\n\t\t\tSystem.out.println(var);\r\n\r\n\t\t} catch (Exception e) {\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\r\n\t}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You could load resources using\u00a0ClassLoader.getResourceAsStream() or\u00a0Class.getResourceAsStream(). Important thing to note is that\u00a0Class.getResourceAsStream (String)\u00a0delegates the call to\u00a0ClassLoader.getResourceAsStream(String). You can see the same below. public InputStream getResourceAsStream(String name) { name = resolveName(name); ClassLoader cl = getClassLoader0(); if (cl==null) { \/\/ A system class. return ClassLoader.getSystemResourceAsStream(name); } return cl.getResourceAsStream(name); } resolveName(name) will add a package name prefix if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-23","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/23","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=23"}],"version-history":[{"count":3,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/23\/revisions"}],"predecessor-version":[{"id":2747,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/23\/revisions\/2747"}],"wp:attachment":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}