title: Read text file that is not in the main package in a runnable jar
body: <p>I have a Swing Java program that reads *.txt files in a resource folder called 'res' containing subfolders of different kind of JSONs. When I run directly from Eclipse, all is fine. If I create an executable jar it doesn't work. I managed to make it work with an horrifying solution, putting all my text files in the package where they are called.</p>
  
  <p>To read them I use this line of code:</p>
  
  <pre><code>InputStream is = this.getClass().getResourceAsStream(file + ".txt");
  </code></pre>
  
  <p>where file is a string. I thought this would search in the jar the specified file but obviously not. I tried to put the path to the files, res/subfolder/name_of_file.txt and subfolder/name_of_file.txt but to no avail.
  Here is the structure of my project</p>
  
  <pre><code>Project
     src
        com.package
     res
        JSON_type1
        JSON_type2
           file.txt
  </code></pre>
  
  <p>From com.package, I have a class that must read from JSON_type2/file.txt. Besides the obvious reason to keep my text file organized in the subfolders is that my GUI will populates a drop down list with the content of those subfolders.</p>
  
  <p>From what I've gather from other questions here, getResourceAsStream() would have been my solution if I wanted to keep my text files in the package where they are called. Other than that I found something about subclassing <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/ClassLoader.html#getResourceAsStream%28java.lang.String%29" rel="nofollow noreferrer">ClassLoader</a> and overwriting <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/ClassLoader.html#findResource%28java.lang.String%29" rel="nofollow noreferrer">findResource()</a>. If possible I want to avoid this solution as I'm far from being familiar with that class and its functionalities. </p>
  
  <p>Should you need more informations, let me know. </p>
  
  <p>Thank you</p>
  
  <p>P.S.: This question arose from my previous question <a href="https://stackoverflow.com/questions/10231544/runnable-jar-not-working-with-referenced-libraries">here</a></p>