title: Using getResourceAsStream is not working in Java
body: <p>I have a very simple method which uses the <code>getclass().getResourceAsStream()</code> method to read a file. However it always returns null and I can't figure out what is wrong. Here is my piece of code.</p>
  
  <pre><code>InputStream sw = getClass().getResourceAsStream("/filename.txt");
  BufferedReader bf = new BufferedReader( new InputStreamReader(sw));
  </code></pre>
  
  <p><code>sw</code> always remain null. the file <code>filename.txt</code> exist in the root directory of my project.        </p>
  
  <p>EDIT:
  I found the reason. I realized that I was running my project from Eclipse and the project was not part of the classpath on my PC. However if I package my program as a jar file and then run it, the files in the jar file are considered as resources and can be read using the getResourceAsStream() method.</p>