{"id":14,"date":"2012-08-04T09:11:36","date_gmt":"2012-08-04T09:11:36","guid":{"rendered":"http:\/\/softwareeveryday.wordpress.com\/?p=14"},"modified":"2012-11-17T11:46:31","modified_gmt":"2012-11-17T11:46:31","slug":"issues-with-loading-properties-files-using-fileinputstream","status":"publish","type":"post","link":"https:\/\/www.softwareeverydayblog.com\/?p=14","title":{"rendered":"Issues with loading properties files using FileInputStream"},"content":{"rendered":"<p><a href=\"https:\/\/www.softwareeverydayblog.com\/wp-content\/uploads\/2012\/08\/paths1.png\"><img decoding=\"async\" class=\"size-full wp-image\" src=\"https:\/\/www.softwareeverydayblog.com\/wp-content\/uploads\/2012\/08\/paths1.png?w=229\" alt=\"Image\" \/><\/a><\/p>\n<p>App.java<\/p>\n<pre lang=\"java\" line=\"1\">\r\npackage com.my;\r\n\r\nimport java.io.FileInputStream;\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\t\tString var = null;\r\n\t\tProperties properties = new Properties();\r\n\t\ttry {\r\n\t\t\tproperties.load(new FileInputStream(\"test.properties\"));\r\n\t\t\tvar = (String) properties.get(\"my.prop\");\r\n\t\t\tSystem.out.println(var);\r\n\t\t} catch (Exception e) {\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>In the above figure folder structure if we run App.java from C:\/TestingAppsresourcetest i.e. the same directory which has test.properties\u00a0using the following command, it will work ok.<\/p>\n<p><strong>C:\/TestingAppsresourcetest&gt;java -cp &#8220;.;target\/classes&#8221; com.my.App<\/strong><br \/>\n<strong>hello<\/strong><\/p>\n<p>However, if we ran App.java from a different directory it would throw an error.<\/p>\n<p><strong>C:\/TestingAppsresourcetesttargetclasses&gt;java com.my.App<\/strong><br \/>\n<strong>java.io.FileNotFoundException: test.properties (The system cannot find the file<\/strong><br \/>\n<strong>specified)<\/strong><br \/>\n<strong> at java.io.FileInputStream.open(Native Method)<\/strong><br \/>\n<strong> at java.io.FileInputStream.&lt;init&gt;(Unknown Source)<\/strong><br \/>\n<strong> at java.io.FileInputStream.&lt;init&gt;(Unknown Source)<\/strong><br \/>\n<strong> at com.my.App.main(App.java:19)<\/strong><\/p>\n<p>So if you use FileInputStream to load properties. Use relative paths to properties files and end up changing the running directory (like we did), then you will face issues.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>App.java package com.my; import java.io.FileInputStream; import java.util.Properties; public class App { public static void main( String[] args ) { String var = null; Properties properties = new Properties(); try { properties.load(new FileInputStream(&#8220;test.properties&#8221;)); var = (String) properties.get(&#8220;my.prop&#8221;); System.out.println(var); } catch (Exception e) { e.printStackTrace(); } } } In the above figure folder structure if we run [&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-14","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/14","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=14"}],"version-history":[{"count":7,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/14\/revisions"}],"predecessor-version":[{"id":189,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/14\/revisions\/189"}],"wp:attachment":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=14"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=14"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=14"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}