{"id":5273,"date":"2014-03-30T20:14:34","date_gmt":"2014-03-30T20:14:34","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/why-does-getresourceasstream-work-in-the-ide-but-not-the-jar-collection-of-common-programming-errors\/"},"modified":"2014-03-30T20:14:34","modified_gmt":"2014-03-30T20:14:34","slug":"why-does-getresourceasstream-work-in-the-ide-but-not-the-jar-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/why-does-getresourceasstream-work-in-the-ide-but-not-the-jar-collection-of-common-programming-errors\/","title":{"rendered":"Why does getResourceAsStream() work in the IDE but not the JAR?-Collection of common programming errors"},"content":{"rendered":"<p>I just want to read a file into my program. The file is located one directory above the working directory at &#8220;..\/f.fsh&#8221;. So the following code runs correctly when I run it in the IDE<\/p>\n<pre><code>String name=\"..\/f.fsh\";\nInputStream is = getClass().getResourceAsStream(name);\nInputStreamReader isreader=new InputStreamReader(is);\/\/CRASHES HERE WITH NULL POINTER EXCEPTION\nBufferedReader br = new BufferedReader(isreader);\n<\/code><\/pre>\n<p>but when I create a JAR file that has f.fsh zipped inside of it and run it, it crashes when creating the InputStreamReader, because the InputStream is null.<\/p>\n<p>I&#8217;ve read a bunch of answers to questions about input streams and JAR files, and what I got out of it is that I should be using relative paths, but I am already doing that. From what I understand getResourceAsStream() can find files relative to the root of the project, that is what I want. Why does it not work in the JAR? What is going wrong, how can I fix it?<\/p>\n<p>Does it have to do with the classpath? I thought that was only for including files external to the jar being run.<\/p>\n<p>I have also tried, but still fail, when putting a slash in:<\/p>\n<pre><code>InputStream is = getClass().getResourceAsStream(\"\\\\\"+name);\n<\/code><\/pre>\n<p>I looked at: How to get a path to a resource in a Java JAR file andfound that contents of a JAR may not necesarily be accesible as a file. So I tried it with copying the file relative to the jar (one directory up from the jar), and that still fails. In any case I&#8217;d like to leave my files in the jar and be able to read them there. I don&#8217;t know what&#8217;s going wrong.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I just want to read a file into my program. The file is located one directory above the working directory at &#8220;..\/f.fsh&#8221;. So the following code runs correctly when I run it in the IDE String name=&#8221;..\/f.fsh&#8221;; InputStream is = getClass().getResourceAsStream(name); InputStreamReader isreader=new InputStreamReader(is);\/\/CRASHES HERE WITH NULL POINTER EXCEPTION BufferedReader br = new BufferedReader(isreader); but [&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-5273","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5273","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5273"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5273\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}