{"id":202,"date":"2011-11-08T00:40:04","date_gmt":"2011-11-08T00:40:04","guid":{"rendered":"http:\/\/hhs1080.hosting.paran.com\/?p=202"},"modified":"2011-11-08T00:40:04","modified_gmt":"2011-11-08T00:40:04","slug":"java-loading-an-xml-file-from-the-classpath","status":"publish","type":"post","link":"https:\/\/blog.goorigo.com\/?p=202","title":{"rendered":"Java : Loading an XML file from the CLASSPATH"},"content":{"rendered":"<p>Today, I faced this issue in Java coding. I needed to load an XML file which was in my classpath but not in the same directory as the classes. The deliverable was a jar and the properties and the configuration XML files were in a different folder and were appended to the classpath at the runtime. The issue was that the file could not be located through the the statement<\/p>\n<p>String fileName = getClass.getSystemResource(&#8220;config.xml&#8221;).getFile;<\/p>\n<p>Then I thought that it checks relative to the current class and so used the statement<\/p>\n<p>String fileName = ClassLoader.getSystemResource(&#8220;config.xml&#8221;).getFile;<\/p>\n<p>But then, it wouldn&#8217;t still recognize the file. The reason is the same, it checks relative to the classes folder. I did not want to get the classpath from the system and browse through it for the config file since the class path could get larger. A couple of google searches and a little research later, I found the solution. The workaround is by using the following statement<\/p>\n<p>String fileName = Thread.currentThread().getContextClassLoader().getResource(&#8220;config.xml&#8221;).getFile;<\/p>\n<p>It only makes sense since in the above statement, you get hold of the context class loader and find the path in the entire classpath&#8230;<\/p>\n<p>POSTED BY KARTHIK AT 6:07 PM<br \/>\n\ucd9c\ucc98 \ud83d\ude15<a href=\"http:\/\/cognitivecache.blogspot.com\/2008\/07\/java-loading-xml-file-from-classpath.html\">http:\/\/cognitivecache.blogspot.com\/2008\/07\/java-loading-xml-file-from-classpath.html<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, I faced this issue in Java coding. I needed to load an XML file which was in my classpath but not in the same directory as the classes. The&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=\/wp\/v2\/posts\/202"}],"collection":[{"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=202"}],"version-history":[{"count":0,"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=\/wp\/v2\/posts\/202\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.goorigo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}